-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
keyof result contains undefined
when used on mapped type with optional properties
#34992
Comments
The 'bug' here is that { [Key in keyof T]: T[Key] extends ValueType ? Key : never; }[keyof T] is interpreted as satisfying the constraint type Example = {
name: "name";
age: never;
} when type ExampleActual = {
name: "name";
age?: undefined;
} and Pick<User, "name" | undefined> and applying The 'correct' fix is that the definition of type PickByValue<T, ValueType> = Pick<T, {
[Key in keyof T]-?: T[Key] extends ValueType ? Key : never;
}[keyof T]>; using the modifier |
Given there's a simple solution that solves the problem, I'm inclined to do nothing further here. It's not clear how we would force an error as @jack-williams suggests, and I'm concerned that doing so would break existing code. |
I asked about that on stackoverflow and I was answered that the case seems to be buggy.
This happens only if strictNullChecks is enabled. It looks like the reason is that when we filter out properties in mapped type using conditional type and
never
, optional property becomesundefined
instead ofnever
. NormallyPick
should reject union containingundefined
but in below case it doesn't. Instead, it returns type with kinda hiddenundefined
key.PickByValue
copied from utility-typesTypeScript Version: 3.7.x-dev.201xxxxx
strictNullChecks
Search Terms:
keyof, undefined
Code
Expected behavior:
Type
UserStrngsKeys
is'name'
Actual behavior:
Type
UserStrngsKeys
is'name' | undefined
Playground Link:
Playground Link
Related Issues:
Not found
The text was updated successfully, but these errors were encountered: