You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typeT=[number]|[number,number];// now t.length has type 1|2lett: T;if(t!.length===0){// compile error: Operator '===' cannot be applied to types '1' and '0'}if(t!.length>100){// no compile error}if(t!.length===1-1){// no compile error}
Expected behavior:
No compile errors on any of these. It's plain inconsistent and not useful.
Actual behavior:
Compile error on t === 0 but not on other comparisons
Playground Link:
See above
Related Issues:
The text was updated successfully, but these errors were encountered:
Tuples have a fixed length as of #17765. If you suspect that t could be a "tuple" of arbitrary length, you could simply use the type number[], since tuples are arrays.
It's true that we could give an error on t.length > 100 because all known lengths are less, but not sure if that would be worth the complexity to add. Same for evaluating constant expressions like 1 - 1; any arithmetic operation on numbers is just treated as a number for simplicity.
For what it's worth, I would love to see arithmetic constraints on things like lengths. But such an error only really becomes useful when users can write such constraints (I envision writing a function like f(valueThatIsNotEmpty: string & { length: number > 0; }) or something).
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
TypeScript Version: 2.7.1
Search Terms: infer, compare, equals
Code
Expected behavior:
No compile errors on any of these. It's plain inconsistent and not useful.
Actual behavior:
Compile error on
t === 0
but not on other comparisonsPlayground Link:
See above
Related Issues:
The text was updated successfully, but these errors were encountered: