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
enumType{A,B,}interfaceTypeMap{[Type.A]: string;[Type.B]: number;}typeX<TextendsType>=TypeMap[T];functionfunc<TextendsType>(type: T,extend: X<T>){if(type===Type.A){// Why "extend" type is not string ?// (parameter) extend: string | number | booleanconsole.log(type,extend);}}func(Type.A,'');func(Type.B,1);
🙁 Actual behavior
In the example, Why "extend" type is not string ?
🙂 Expected behavior
The "type" constrained to "Type.A",so "extend" shuld be "string".
Activity
MartinJohns commentedon Sep 12, 2024
Because checking what value
typeis does not have any relevance for what type the value ofextendis.This is a perfectly valid call:
func<Type.A | Type.B>(Type.A, 1);lengyuxuan commentedon Sep 12, 2024
How should I constrain the type of "extend" ? Do you have any suggestions ? Thank you so much !
jcalz commentedon Sep 12, 2024
Duplicate #33014
For now, if you need this behavior you either need to rewrite in terms of #47109 and use generic indexing instead of control flow anlaysis:
or, you need to use control flow analysis on a discriminated union and not use generics:
Playground link to code
lengyuxuan commentedon Sep 13, 2024
Good job