microsoft / microsoft/TypeScript

Narrow length of tuples

Aperta
#33,182 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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 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

declare function bytesToInt(bytes: Array<number> & {length:4}): number
bytesToInt([1,2,3,4]) // currently errors, would be nice if it didn't.
bytesToInt([1,2,3,4] as Array<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.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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.

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

La issue non nomina file, test o punti di ingresso. Inizia riproducendo l'esempio bytesToInt in un test case TypeScript e analizza come viene rappresentato e verificato length delle tuple. Il lavoro è completato quando la chiamata con la tupla viene accettata senza un cast, mentre il comportamento esistente di tuple e array rimane invariato.

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

Valutazione

Stack tecnologico
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
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.