microsoft / microsoft/TypeScript
Index signatures in type definitions: now we have `noUncheckedIndexedAccess`, include or omit `undefined`?
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
lib Update Request
Before we had noUncheckedIndexedAccess, it was common practice to add undefined to an index signature as a workaround. For example:
- https://github.com/microsoft/TypeScript/issues/13792 in which the index signature inside
DOMStringMap(lib.dom.d.ts) was modified to includeundefined. - https://github.com/DefinitelyTyped/DefinitelyTyped/pull/43931 in which all index signatures in
@types/nodewere modified to includeundefined(e.g.ProcessEnvandParsedUrlQuery).
However, it seems this workaround wasn't extended to all index signatures, e.g.
RegExpMatchArray['groups'] in lib.es2018.regexp.d.ts and PropertyDescriptorMap in lib.es5.d.ts.
Now we have noUncheckedIndexedAccess I would like to ask what the best way forward is regarding these workarounds? For people who enable noUncheckedIndexedAccess, these workarounds are redundant. For people who don't enable noUncheckedIndexedAccess, these workarounds are still necessary—but potentially not always desirable since it forces a higher level of strictness.
It seems inconsistent that some index signatures use this workaround and others do not, so I think we should decide either to use this workaround in all index signatures or remove it from all index signatures and suggest people use noUncheckedIndexedAccess.
Including undefined creates a few problems, for example:
declare const v: DOMStringMap;
Object.values(v).map((value) => {
// Expected: string;
// Actual: string | undefined; ❌
value;
});
for (const value of Object.values(v)) {
// Expected: string;
// Actual: string | undefined; ❌
value;
}
const test: DOMStringMap = {
// This should error
foo: undefined
}
If the TypeScript team can decide the best way forward then this will also provide some guidance to the community—for example we're trying to decide what the best option is for @types/node: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/51177.
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 comparer les signatures d’index dans lib.es2018.regexp.d.ts, lib.es5.d.ts et lib.dom.d.ts, puis examinez les exemples de l’issue concernant noUncheckedIndexedAccess. L’issue ne précise pas quelle politique adopter ; il faudrait donc que l’équipe prenne une décision avant de mettre à jour les définitions concernées et de vérifier son impact sur le comportement cité.
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é
- À clarifier
- Accessibilité débutants
- 25/100