-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
The type of inference is inaccurate #59361
Comments
Inference is based on a set of heuristics. Currently, TS eliminates matching types here (based on This is a "correct answer" since |
For example, in the following two examples, I still can't know why the result of the first example is unknown instead of string, and why I can't get a more precise type like the second example. type MyType<T> = T extends (infer R) & string ? R : never
type Result = MyType<string> // unknown type MyType<T> = T extends (infer R) & string ? R : never
type Result = MyType<'abc'> // abc |
It's just how TypeScript works today. It has some algorithm for matching between your source (type argument here) and the targets (the intersection here). There is no better answer to that - it's an implementation detail. It's not incorrect though. Those examples aren't really practical, so I'm not sure what you're trying to solve here. I understand that it might be confusing and interesting why they behave differently but that's about it. I'm sure that this part of the algorithm was written to improve some more real-life scenarios for which that selection mechanism makes sense more often than not. |
This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
🔎 Search Terms
infer unknow
🕗 Version & Regression Information
⏯ Playground Link
No response
💻 Code
// Your code here
🙁 Actual behavior
Why is the Result here unknown instead of string? Doesn't infer infer a more precise type? If not, then why is Result abc instead of string when T is abc?
🙂 Expected behavior
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: