microsoft / microsoft/TypeScript
Infer a tuple type instead of array type when possible
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Suggestion
I'd prefer typescript to be more aggressive about inferring tuple types, instead of always defaulting to Array.
This is related to https://github.com/microsoft/TypeScript/issues/6574. I'd like to open up a discussion about this again since that thread is locked, and conversation stopped after a workaround was provided without completely addressing the general issue.
🔍 Search Terms
Typescript, tuple, tuples, map, inference
✅ 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.
Existing typescript code could start emitting type errors from this change, so it would make sense to provide this as an option in tsconfig.
⭐ Suggestion
Typescript should infer a type to be a tuple instead of an array when it's possible.
📃 Motivating Example
The specific example I have in mind is for Array.prototype.map(), but there are probably more.
const tuple: [number, number, number] = [1, 2, 3];
const result = tuple.map(value => value + 1);
Here, typescript infers the type of result to be number[] rather than [number, number, number]. This could result in some wonkiness when trying to access values inside the tuple:
const [a, b, c, d] = result; // Typescript doesn't detect an error since result is an Array type.
const e = result[10]; // Typescript doesn't detect an error since result is an Array type.
Both of these errors can be caught if Typescript instead inferred result to be the tuple type [number, number, number].
💻 Use Cases
The above example is a use case. I want typescript to tell me when I make errors in my code.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia esaminando il comportamento di inferenza di TypeScript per Array.prototype.map() e la discussione precedente nell'issue #6574. Definisci quando una tupla mappata dovrebbe mantenere il proprio tipo tupla, inclusa l'opzione tsconfig proposta, e aggiungi test che mostrino che l'accesso non valido alla tupla viene rifiutato, mentre i risultati mappati validi continuano a funzionare.
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
- 25/100