-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Incorrect type inference on union type, unless using a third property as a discriminant #57758
Comments
Essentially a duplicate of #30581. Not sure why that is closed. |
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 Using some intersections we can fix the latter: TS playground. It still doesn't infer |
kudos to @ssalbdivad: TS playground it's not particularly intuitive how to put all the pieces together but this one works very well |
This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
"inference" "inference narrow" "inference discriminant" "inference union"
π Version & Regression Information
Tested on multiple versions, this is not a new bug.
β― Playground Link
https://www.typescriptlang.org/play?ts=5.4.2#code/C4TwDgpgBA6gTgewHYHMCSSBmE4SQY2gF4oBvAKAEgBbAVwBtgBLMeiALimDlogG4qANwCG9XpwDO3JqgDaAXQGV8o+gCNh+ANacAFAH1J0ufICUUIgD4oghEwAmAgL5QAPmSp1GLNp0yiJfiFRcSgpOBkUJRV6dU0dKAMjCNRzKxs7R3InAXJMWgJmZCg2YABVCQxsOF1hTnhkdCwcPEJzCkphADoYuO1arpExCFMlJkxE7q9mVhGPSk6e1Q1+7qHeUaoXCHpA+cXela0B9ZGlJ2zyIA
π» Code
π Actual behavior
Typescript does not recognise that
callback
will always acceptvalue
as a parameter, since they both use eitherstring
orstring[]
.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.Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: