-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Inconsistent behavior between intersection literal type during inference #57776
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
Comments
What would β |
I'm still articulating that. Basically it returns string literal types like Then you can do It should be relatively easy to do inside TS as it has that info in the AST. The problem of that approach is how to handle intersection types. Another way is to provide type utils like There are some variants need to be handled such as |
type R6 = 1 & { a: 1 } extends 1 & infer U ? U : never // { a: 1 }
type R7 = 1 & { a: 1 } extends number & infer U ? U : never // 1 & { a: 1 }
|
fwiw, technically the inference for |
Ah, ye - totally. I certainly didn't want to imply that this is a bug - just why this is different today at the implementation level. |
understand. It depends on what is the definition of The bottomline for me is whether if we have a not-too-hacky-way to build the types we want. |
The general behavior of |
Understand that this is not a defect. It's the same as @Andarist pointed out above. Maybe the question is how it should behave when it comes to sets like number vs numeric literals, string is string literals, boolean vs true/false. The question is twofold: consistency and application limitation. At the moment, this type P1 = number & { a: 1 } extends number ? true : false // true
type P2 = 1 & { a: 1 } extends number ? true : false // true
type P3 = number & { a: 1 } extends number & infer U ? U : never // { a: 1 }
type P4 = 1 & { a: 1 } extends number & infer U ? U : never // 1 & { a: 1 } Yes, we can explain that the implementation is using exact match, but that does not help user to reason the type. To be consistent, both Using If there is an alternative way to do that, or have a way to address the string template literal issue mentioned in the OP #54648, that would work. |
This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Please consider re-open this. The inconsistency makes it not possible to write reusable generic types that work with intersection types. |
π Search Terms
literal intersection infer extract value
π Version & Regression Information
Behavior changed since 5.1
β― Playground Link
https://www.typescriptlang.org/play?ts=5.5.0-dev.20240314#code/C4TwDgpgBASgjFAvFAdgVwLYCMICcoQAewEKAJgM6qY74D8Uwua0AXFAGYCGANhdAHoBjZhABQoSLABMSKAiIlyVBAyYso7bn0HD14ydBgBmOQuKlK1bHihrRmzr35QhIlhPBGALHPQ38ADIoAG8oLnYEAF8CC2VrWjt3NicdVz1RTykYAFY-GltgsIj5KBjFSyp-ROCASxQOWwBVJJb2FAgAN1s3YsiyrKMANjMoIvD+8rirBDqG5tbHDu78XonSqMHYAHZR8ZLo2KUrasKoesb8FoY21C6e4VnQ9eigA
π» Code
π Actual behavior
see above
π Expected behavior
R7
should returns{ a: 1 }
as1 extends number
.Due to this and #54648 (comment),
currently it doesn't seem to have a way to write the
IsNegative
type (among others) anymore.This is one of the problems caused by #54648. It would be great if that can be re-evaluated.
Additional information about the issue
tbh, IMO many types I wrote in type-plus would be best provided by TS itself to make the type system more predictable and enable us to write better types. One suggestion is to support
typeof T
for types, but that's a different topic.The text was updated successfully, but these errors were encountered: