microsoft / microsoft/TypeScript

Declare TypedArray length

Aperta
#45,547 6 commenti 6 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia confrontando il comportamento richiesto per i TypedArray di lunghezza fissa con i tipi tupla esistenti e le dichiarazioni correnti di TypedArray di TypeScript. Definisci come deve essere verificato il tipo dell’esempio del vettore, incluso se sono supportate solo le lunghezze minime o anche quelle esatte, e aggiungi test che mostrino che le lunghezze incompatibili di TypedArray vengono rifiutate.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.