Skip to content
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

Open
5 tasks done
EnderShadow8 opened this issue Aug 23, 2021 · 6 comments
Open
5 tasks done

Declare TypedArray length #45547

EnderShadow8 opened this issue Aug 23, 2021 · 6 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@EnderShadow8
Copy link

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.

@andrewbranch
Copy link
Member

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 as type assertion: playground. Are there severe limitations to an approach like this when brought into the real world?

@andrewbranch
Copy link
Member

Not exactly recommended, but here’s some hacks to make it work for plain old new Float32Array(length). I don’t think we’d ever add something like this to standard type definitions, but the fact that there are workarounds for cases like these means we’re probably less likely to add something specific to the language for it.

@fatcerberus
Copy link

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 v1 could be undefined. I finally realized that it actually meant v1[0] might be undefined, but there's no object type involved there - the result of the indexing is number | undefined. It should probably say "value might be undefined" instead.

@andrewbranch andrewbranch added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Aug 25, 2021
@amw
Copy link

amw commented Oct 1, 2021

I tried to hack something like this, but it doesn't work the way I prayed it would:

type RGBATuple = [number, number, number, number]
type TypedRGBATuple = Uint8ClampedArray & RGBATuple
const data = new Uint8ClampedArray(4) as TypedRGBATuple
const color = new Color(...data)
// produces error
// A spread argument must either have a tuple type or be passed to a rest parameter. ts(2556)

It's a bit odd since TypeScript should know that data is also a tuple.

@GoToLoop
Copy link

GoToLoop commented Jan 13, 2023

I can't pass a spread TypedArray to a function! =(

A spread argument must either have a tuple type or be passed to a rest parameter.(2556)

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.

https://www.TypeScriptLang.org/play?target=9#code/JYWwDg9gTgLgBDAnmApnMBWOAzKERwBEmhAUKSgB6SxwDGEAdgM7wCCAMhwPoDCA8h34AlAMpwAvHAAMlbNjgAeRTIgBmaeQYt4AQyhQAHACFg8KYxQB3OAFVgjGIbYHdiABQBJR2oBMLqDcAOmMATQAVAFFRbgAFSOFuSI5IgFlIgDlwgEpSbVY4ACMAV2wANWBrSThLGwARXRhdCut3fSNTGCCS+RQoXIpqaHhsYsY6GGAmeggAG2gAMTwQP0KzcIgTM2Z3MAAudAwAGnoDxmKQQr7suABvUjhHotKWqyDmFBhvGD93aRO6LknnAoJ9ilBGOgggx5lB3EEEe0tl1mLNgHQUO4AIzZE5IzoAbWkAF1cgBfchUGgjMYTKaQwKMAAm+F4c2guwOmBu92BoJg4MhMMWy1W602nR2YBOYCCjJZIHcnB4AiEYmy5NIQA

@GoToLoop
Copy link

GoToLoop commented Jan 13, 2023

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.

https://www.TypeScriptLang.org/play?target=9#code/JYWwDg9gTgLgBDAnmApnMBWOAzKERwBEmhAUKSgB6SxwDGEAdgM7wCCAMhwPoDCA8h34AlAMpwAvHAAMlbNjgAeRTIgBmaeQYt4AQyhQAHACFg8KYxQB3OAFVgjGIbYHdiABQBJR2oBMLqDcAOmMATQAVAFFRbgAFSOFuSI5IgFlIgDlwgEpSbVY4ACMAV2wANWBrSThLGwARXRhdCut3fSNTGCCS+RQoXIpqaHhsYsY6GGAmeggAG2gAMTwQP0KzcIgTM2Z3MAAudAwAGnoDxmKQQr7suABvUjhHotKWqyDmFBhvGD93aRO6LknjMdHAANpwXQnKAnADmJ0KcAAutV2lsYA8nlBPsUoIx0EEGPMoO4YXB4UUTrpcgBfchUGgjMYTKb4wKMAAm+F4c2guwOmBu92B2JguPxRMWy1W602nR2YBOYCC7K5IHcnB4AiEYmytKAA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants