microsoft / microsoft/TypeScript

Index signatures in type definitions: now we have `noUncheckedIndexedAccess`, include or omit `undefined`?

Aperta
#42,810 6 commenti 6 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

lib Update Request

Before we had noUncheckedIndexedAccess, it was common practice to add undefined to an index signature as a workaround. For example:

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.

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

Inizia confrontando le firme di indicizzazione in lib.es2018.regexp.d.ts, lib.es5.d.ts e lib.dom.d.ts, quindi esamina gli esempi dell’issue relativi a noUncheckedIndexedAccess. L’issue non specifica quale criterio adottare, quindi sarebbe necessaria una decisione del team prima di aggiornare le definizioni interessate e verificarne l’impatto sul comportamento citato.

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
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.