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
{{ message }}
Repository navigation
keyof "a" behaves differently when using a type parameter #23345
This is working as intended. When instantiating a homomorphic mapped type such as { [P in keyof T]?: boolean } we distribute over union types and leave non-object types unchanged. For example, Indirect<{ x: string } | undefined> is resolved as Indirect<{ x: string }> | undefined. For this reason, Indirect<"a"> is simply "a".
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
we distribute over union types and leave non-object types unchanged
What does it mean to "distribute over union types"?
Also, it seems that as long as I avoid using keyof inside the generic "mapped type" declaration, I can avoid this footgun. See here: https://goo.gl/DakrYm
What's the practical benefit of making keyof behave differently when inside a generic "mapped type"? Or is it simply a consequence of the current implementation?
Activity
ahejlsberg commentedon Jun 3, 2018
This is working as intended. When instantiating a homomorphic mapped type such as
{ [P in keyof T]?: boolean }we distribute over union types and leave non-object types unchanged. For example,Indirect<{ x: string } | undefined>is resolved asIndirect<{ x: string }> | undefined. For this reason,Indirect<"a">is simply"a".typescript-bot commentedon Jun 18, 2018
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
aleclarson commentedon Sep 13, 2018
What does it mean to "distribute over union types"?
Also, it seems that as long as I avoid using
keyofinside the generic "mapped type" declaration, I can avoid this footgun. See here: https://goo.gl/DakrYmWhat's the practical benefit of making
keyofbehave differently when inside a generic "mapped type"? Or is it simply a consequence of the current implementation?