microsoft / microsoft/TypeScript
Declare TypedArray length
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Suggestion
🔍 Search Terms
typedarray length tuple typed array
✅ Viability 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, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
A way to declare a fixed length TypedArray similar to Tuples.
📃 Motivating Example
Consider a hypothetical vector library, similar glmatrix:
function addVector2(out: Float32Array, v1: Float32Array, v2: Float32Array) {
out[0] = v1[0] + v2[0]
out[1] = v1[1] + v2[1]
return out
}
There's currently no way to type check that the arguments are indeed Vector2's and not some other Float32Arrays. This is possible with Tuple types though using regular arrays. Since TypedArrays are inherently fixed length even at runtime, it makes sense for there to be some way of declaring length. Even if this is just a minimum length, that would be a huge improvement since that guarantees out of bounds indexing safety.
💻 Use Cases
Many uses of TypedArrays in JS make assumptions about length. But the main one would probably be vectors, matrices, quarternions, etc.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by comparing the requested fixed-length TypedArray behavior with existing tuple types and TypeScript's current TypedArray declarations. Define how the vector example should be type-checked, including whether only minimum lengths or exact lengths are supported, and add coverage showing that incompatible TypedArray lengths are rejected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100