-
Notifications
You must be signed in to change notification settings - Fork 12.2k
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
Declare TypedArray length #45547
Comments
Some real-world examples might be helpful here. It feels to me like TS provides 95% of what you need, and you can fake the last 5% with an |
Not exactly recommended, but hereβs some hacks to make it work for plain old |
Not directly related to the issue, but why does the error message in that playground say "Object is possibly undefined"? I was confused for a while because I couldn't figure out why it thought |
I tried to hack something like this, but it doesn't work the way I prayed it would:
It's a bit odd since TypeScript should know that |
I can't pass a spread TypedArray to a function! =(
import type p5 from "p5"
export const ALL_COLORS = 0xff << 0o30
const arr8Bit = new Uint8Array(Int32Array.BYTES_PER_ELEMENT)
const bufView = new DataView(arr8Bit.buffer)
export function colorFrom32bitTo8Bits(p: p5, c: number) {
bufView.setInt32(0, c)
return p.color(...arr8Bit.slice(1), arr8Bit[0])
}
export function randomColor(p: p5) {
return colorFrom32bitTo8Bits(p, p.random(ALL_COLORS))
} Can't neither coerce it to a tuple nor make TS know its length. |
Well, as a workaround I've had to use Array destructuring assignment for the TypedArray: import type p5 from "p5"
export const ALL_COLORS = 0xff << 0o30
const arr8Bit = new Uint8Array(Int32Array.BYTES_PER_ELEMENT)
const bufView = new DataView(arr8Bit.buffer)
export function colorFrom32bitTo8Bits (p: p5, c: number) {
bufView.setInt32(0, c)
const [ a, r, g, b ] = arr8Bit
return p.color(r, g, b, a)
}
export function randomColor (p: p5) {
return colorFrom32bitTo8Bits(p, p.random(ALL_COLORS))
} Not ideal though. But I can't see any fix in TS for it any time soon. |
Suggestion
π Search Terms
typedarray length tuple typed array
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
A way to declare a fixed length TypedArray similar to Tuples.
π Motivating Example
Consider a hypothetical vector library, similar glmatrix:
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.
The text was updated successfully, but these errors were encountered: