microsoft / microsoft/TypeScript
Index signatures in type definitions: now we have `noUncheckedIndexedAccess`, include or omit `undefined`?
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginnen Sie mit dem Vergleich der Indexsignaturen in lib.es2018.regexp.d.ts, lib.es5.d.ts und lib.dom.d.ts, und prüfen Sie anschließend die Beispiele des Issues zu noUncheckedIndexedAccess. Im Issue ist nicht festgelegt, welche Richtlinie übernommen werden soll; daher wäre vor der Aktualisierung der betroffenen Definitionen und der Überprüfung ihrer Auswirkungen auf das genannte Verhalten eine Entscheidung des Teams erforderlich.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 25/100