Skip to content

Commit

Permalink
fix: remove optional status from KeysByType keys
Browse files Browse the repository at this point in the history
Because status modifiers are attached to keys, `KeysByType` gives back
an `undefined` key when the original object contains optional keys; even
if the optional key is not of the given type. This change fixes that by
removing the optional status modifier from the object while iterating
over the keys.

The downside to this change is that it does not perfectly purify the
object. So if other status modifiers are added in the future, we will
not necessarily be robust to that change. A more "pure" fix (pun
intended) would be to work with a purified object. The downside is that
the only way I could figure out to do that would require much uglier
code and/or a helper function that would need to be exported. In favor
of minimizing "private" exports, I opted for the simple solution.

Also a downside here is that we cannot filter by `undefined` if a key
is optional. This was already an issue before this commit, so I'm
punting on it for now because the fix seems non-trivial.

Closes #106
  • Loading branch information
andnp committed Jun 25, 2019
1 parent 92a9ca8 commit a9eaa4b
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/types/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export type DeepReadonly<T> = Readonly<{
* @returns keys of `O` whose right-side value is `T`
*/
export type KeysByType<O extends object, T> = {
[k in keyof O]: O[k] extends T ? k : never;
[k in keyof O]-?: O[k] extends T ? k : never;
}[keyof O];


Expand Down

0 comments on commit a9eaa4b

Please sign in to comment.