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
Narrow the length property on tuple types when used as an Array.
Use Cases
Often times a method expects an array of some particular length. This can be expressed as [T, T, T] where the number of tuple items is the length of the array. However, sometimes the length is itself generic and in order to properly constrain the type you need to type the parameter as Array<T> & {length:L}. The problem with this is that if you have a tuple of length L, the compiler still assumes that the length is of type number.
It would be useful if a tuple type correctly narrowed the type of length to be the number of elements in the tuple. This way you could pass a tuple into a method that expects a fixed length array without casting.
Examples
declarefunctionbytesToInt(bytes: Array<number>&{length:4}): numberbytesToInt([1,2,3,4])// currently errors, would be nice if it didn't.bytesToInt([1,2,3,4]asArray<number>&{length:4})// error prone and something the compiler could do for you.
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript/JavaScript code
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
It would be super cool if TypedArrays could get this behavior, since their length is fixed at construction time. I'm not sure exactly how to achieve this, so I'll leave it out of this suggestion but if it is possible for new Uint8Array(4) to result in a type Uint8Array & {length:4} that would be great.
The text was updated successfully, but these errors were encountered:
@AnyhowStep What problems are caused (with regards to this feature request) by JavaScript supporting sparse arrays? I recognize that sparse arrays can certainly cause problems, but that seems to be true unrelated to this feature request.
Looks like the problem here is that {length: 4} doesn't prompt the compiler not to widen the type of the array literal [1, 2, 3, 4] to number[]. There are various tricks used to prevent the widening of the types of input parameters, and & {length: 4} isn't one of them. But (Array<T> | [T])is, so you could do this:
Note that without that & {} the function infers L from both length and tuple, which would give you something like 2 | 4 for genericBytesToNum(2, [1,2,3,4]), defeating the purpose. You want tuple to be non-inferential, and & {} is one way to get this behavior.
Search Terms
tuple length narrow
Suggestion
Narrow the
length
property on tuple types when used as an Array.Use Cases
Often times a method expects an array of some particular length. This can be expressed as
[T, T, T]
where the number of tuple items is the length of the array. However, sometimes the length is itself generic and in order to properly constrain the type you need to type the parameter asArray<T> & {length:L}
. The problem with this is that if you have a tuple of length L, the compiler still assumes that thelength
is of typenumber
.It would be useful if a tuple type correctly narrowed the type of
length
to be the number of elements in the tuple. This way you could pass a tuple into a method that expects a fixed length array without casting.Examples
Checklist
My suggestion meets these guidelines:
It would be super cool if TypedArrays could get this behavior, since their
length
is fixed at construction time. I'm not sure exactly how to achieve this, so I'll leave it out of this suggestion but if it is possible fornew Uint8Array(4)
to result in a typeUint8Array & {length:4}
that would be great.The text was updated successfully, but these errors were encountered: