From a9eaa4b0353d9b8a4f6254d7b8c9515c5b7f464a Mon Sep 17 00:00:00 2001 From: andy Date: Fri, 14 Jun 2019 12:03:38 -0600 Subject: [PATCH] fix: remove optional status from KeysByType keys 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 --- src/types/objects.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/objects.ts b/src/types/objects.ts index b86ba4b..1c9f64d 100644 --- a/src/types/objects.ts +++ b/src/types/objects.ts @@ -219,7 +219,7 @@ export type DeepReadonly = Readonly<{ * @returns keys of `O` whose right-side value is `T` */ export type KeysByType = { - [k in keyof O]: O[k] extends T ? k : never; + [k in keyof O]-?: O[k] extends T ? k : never; }[keyof O];