-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Consistent intersection type with array and tuple #50346
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
// suggested behavior
type T = number[] & [number]
let t: T[1] // should be error or never
let len: T["length"] // 1 I think this goes back to what The same subtyping/subsetting behavior might be observed on the indexing expression itself -- if |
I agree but I think |
I understood! As such operation have to be applicable. But its result has to obey the covariance property- |
Chiming in here to leave an opinion. I'm implementing a type system for an unrelated project, and I've been sanity checking it by seeing how TypeScript handles these cases. I consider the following to be inconsistent behavior: type Error = [1] & [1, 2]; // never
type Valid = { a: 1 } & { a: 1, b: 2 }; // { a: 1, b: 2 } If differing Given how useful |
Suggestion
π Search Terms
array, tuple, intersection, covariance, #32926, #33760
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
I think this is rather theoretical approach for the behavior of intersection types and covariance for the consistency.
If this doesn't meet the design policy, feel free to close this.
π Motivating Example
Comprehension check for the intersection type and covariance
With my understanding of TypeScript and set theory, intersection type seems to obey following:
Also covariance/contravariance can alter the direction of such relation.
Current behavior of finite/infinite computed property
Now consider the following object types
This makes sense. Note that this somehow behaves contravariant, while with the following behaves covariant.
Here is the #playground for a glimpse.
What should be
T[] & [T]
?I already read this from #33760 (comment) . But with the theoretical point of view, I just wonder whether this is helpful for the consistent implementation for future or not.
I don't know the implementation detail of this, but along the empirical tests, I think
[T]
is considered like:And
T[]
resembles:Currently, with TypeScript v4.7.4, the result is a little bit weird. #playground
Is this because
0
is not computed property? How about[T, T]
or[T, T, T]
?π» Use Cases
Literal type
0
should be considered as finite subset ofnumber
, and by "Current behavior of finite/infinite computed property", the computed property of index, should be narrowed into0
. Note thatlength
is already narrowed into1
.I understand that this might impractical, and nobody do
number[] & [number]
. But I'm not sure that, there is no guarantee for other complex composition never make such result. I hope this insight would be helpful for TypeScript team.The text was updated successfully, but these errors were encountered: