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
Incorrect type inference on union type, unless using a third property as a discriminant #57758
typeSelection={multiple: true;value: string[];callback: (_: string[])=>void;}|{multiple: false;value: string;callback: (_: string)=>void;};functiontest(a: Selection){// Error: Typescript does not recognise that `value` and `callback` use the same type.a.callback(a.value);if(a.multiple){// Valid: Typescript narrowed down the type to `string[]`a.callback(a.value);}else{// Valid: Typescript narrowed down the type to `string`a.callback(a.value);}}
🙁 Actual behavior
Typescript does not recognise that callback will always accept value as a parameter, since they both use either string or string[].
Interestingly, Typescript can infer the types properly using the if (multiple) { ... } condition.
🙂 Expected behavior
All three callback(value) calls should be valid, from a type point of view.
It's probably closed because it has been determined that it would be too computationally expensive to do it and the current best solution is to use the "indexed mapped type as a union" pattern from TS 4.6 (see here).
However, this discriminant value isn't easily used as a property key so while we can make the internal implementation of a function happy about it... it still doesn't quite infer a concrete K that is used and it doesn't discriminate the input argument for contextual parameters etc: TS playground
Using some intersections we can fix the latter: TS playground. It still doesn't infer K though and I'm not sure how to make true/false a viable inference source for "true"/"false"
Activity
MartinJohns commentedon Mar 13, 2024
Essentially a duplicate of #30581. Not sure why that is closed.
Andarist commentedon Mar 13, 2024
It's probably closed because it has been determined that it would be too computationally expensive to do it and the current best solution is to use the "indexed mapped type as a union" pattern from TS 4.6 (see here).
However, this discriminant value isn't easily used as a property key so while we can make the internal implementation of a function happy about it... it still doesn't quite infer a concrete
Kthat is used and it doesn't discriminate the input argument for contextual parameters etc: TS playgroundUsing some intersections we can fix the latter: TS playground. It still doesn't infer
Kthough and I'm not sure how to maketrue/falsea viable inference source for"true"/"false"Andarist commentedon Mar 13, 2024
kudos to David Blass (@ssalbdivad): TS playground
it's not particularly intuitive how to put all the pieces together but this one works very well
fatcerberus commentedon Mar 14, 2024
It looks like #30581 was auto-closed when #47109 got merged.
typescript-bot commentedon Mar 18, 2024
This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes.