You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Search Terms:
indexed access type, index type never
Code
typeExample<TextendsRecord<'a',string>>=T['a']typeResult1=Example<{a: "x"}|{a: "y"}>// type of Result1 is "x" | "y"typeResult2=Example<{a: "x"}>// type of Result2 is "x"typeResult3=Example<never>// type of Result3 is any
That actually only works because of how conditional types work with type parameters. It distributes the condition over any unions, and never is treated as an empty union. So it actually isn't that never is failing the condition there, it's that the condition is never even tested. It would actually pass the condition and end up as any if the condition is tested.
You can see that's what is happening more clearly with something like
typeExample2<T>=TextendsRecord<'a',string> ? true : false;typeResult2=Example2<never>// never instead of either true or false
And if you get rid of the distribution behavior by wrapping the two sides in [] you can see you still get any:
typeExample3<TextendsRecord<'a',string>>=[T]extends[Record<'a',string>] ? T["a"] : never;typeResult3=Example3<never>// any
TypeScript Version: 2.9.0-dev.20180329
Search Terms:
indexed access type, index type never
Code
Expected behavior:
Type of
Result3
to benever
Actual behavior:
Type of
Result3
isany
Playground Link:
Link
Workaround
Now that #22042 is resolved this can be worked around with
Related Issues:
Similar to #22042. That was about type of
T[K]
whenK
wasnever
while this is about whenT
isnever
.The text was updated successfully, but these errors were encountered: