microsoft / microsoft/TypeScript
Add undefined to tuple index signature, or reject keys that are not "keyof tuple"
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
Search Terms
Tuple index signature
Suggestion
I am opening up this issue as Seperate from https://github.com/microsoft/TypeScript/issues/13778 since I am ok with the current interpretation of the Array.
declare var strArray: string[] // infinitely long list of type string
var a : strArray[10 as number] // ok; a is string
but this fails in conjunction with tuples
declare var strTuple: [number, number] // length = 2
var a : strTuple[10 as number] // absolutely not ok; a is string;
Better Options:
var a : strTuple[10 as number] should either be string | undefined or it should give a type error similar to:
var a = { 0: 10, 1: 20} as const;
a[10 as number]; // type 'number' can't be used to index type
Tuples are already aware of boundaries and properly return undefined for more specific number types:
const t0: [string][0] = 'hello' // ok
const t1: [string][1] = 'hello' // Type '"hello"' is not assignable to type 'undefined'.
// therefore this is already true!
const t2: [string][0|1] // string | undefined
why would type number act any differently?
Use Cases
// Easy fix for for loops
for (let i = 0 as 0 | 1; i < 2; i++) {
var definitly_string = strTuple[i] // type string
var j = i + someNumber;
var not_definitly_string = strTuple[j] //lets you know you need to be careful
}
You could use keyof typeof strTuple instead of 0 | 1 when you fix this https://github.com/microsoft/TypeScript/issues/27995
for (let i = 0 as keyof typeof strTuple; i < strTuple.length; i++) {
var definitly_string = strTuple[i] // type string
var j = i + someNumber;
var not_definitly_string = strTuple[j] //lets you know you need to be careful
}
Given that strictNullChecks still allow typed arrays to do this:
var notString1 = strArray[Infinity]; // no error, not undefined
var notString2 = strArray[NaN]; // no error, not undefined
It would be nice to have a mechanism that could allow you to declare arrays with better type safety.
Examples
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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par reproduire les exemples d’indexation de tuples dans l’issue, notamment les cas numériques, hors limites et keyof, et comparez-les avec l’indexation des tableaux. Évaluez les deux résultats proposés : ajouter undefined à l’accès aux tuples indexés numériquement ou rejeter les clés qui se trouvent en dehors de keyof du tuple. Le travail est considéré comme terminé lorsque le comportement choisi est spécifié de manière cohérente et couvert pour les exemples présentés.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 30/100