Skip to content
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

Closed
Zackin-He opened this issue Jul 19, 2024 · 4 comments
Closed

The type of inference is inaccurate #59361

Zackin-He opened this issue Jul 19, 2024 · 4 comments
Labels
Not a Defect This behavior is one of several equally-correct options

Comments

@Zackin-He
Copy link

🔎 Search Terms

infer unknow

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

No response

💻 Code

// Your code here

🙁 Actual behavior

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

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

type MyType<T> = T extends (infer R) & string ? R : never
type Result = MyType<string> // Expected string

Additional information about the issue

No response

@Zackin-He Zackin-He changed the title infer unknow The type of inference is inaccurate Jul 19, 2024
@Andarist
Copy link
Contributor

Inference is based on a set of heuristics. Currently, TS eliminates matching types here (based on isTypeIdenticalTo) and when either targets or sources are emptied... it returns. Later on, it picks up the constraint of your type (unknown) variable in the absence of inference candidates.

This is a "correct answer" since unknown & string is still string

@Zackin-He
Copy link
Author

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.
Where did the targets or sources and inference candidates you mentioned come from.

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

@Andarist
Copy link
Contributor

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.

@RyanCavanaugh RyanCavanaugh added the Not a Defect This behavior is one of several equally-correct options label Jul 26, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not a Defect This behavior is one of several equally-correct options
Projects
None yet
Development

No branches or pull requests

4 participants