microsoft / microsoft/TypeScript

Types with optional props get implicit index signatures that cannot be made explicit

Aperta
#59,392 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 Search Terms

implicit index signatures, explicit index signatures, optional partial undefined,

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about index signatures and undefined
⏯ Playground Link

Playground link

💻 Code
// @strict: true
// @exactOptionalPropertyTypes: false
// @noUncheckedIndexedAccess: false

type ObjImplicit = {
    a?: string | undefined,
    b: string
};
type Idx = { [k: string]: string }
declare const o: ObjImplicit;
const i: Idx = o; // allowed because A has implicit index signature

type ObjExplicit = {
    a?: string | undefined, // error! not allowed
    //~ <-- Property 'a' of type 'string | undefined' is not assignable to 'string' index type 'string'.
    b: string,
    [k: string]: string
}
🙁 Actual behavior

{ a?: string | undefined } can implicitly have the index signature {[k: string]: string} but cannot explicitly have such an index signature

🙂 Expected behavior

I think I expected the implicit index signature to fail?

Additional information about the issue

Without --exactOptionalPropertyTypes, {a?: string} and {a?: string | undefined} are the same type. I figured that would mean both implicitly and explicitly adding index signatures would either require or not require undefined.

We also have the situation where Partial<{[k: string]: string}> becomes {[k: string]: string | undefined} instead of {[k: string]: string}, which makes me lean toward "require undefined", for consistency. Currently the discrepancy leads to questions like this one.

Anyway, I expect this to not be a bug, but intended behavior that flew under my radar and which I cannot see explicitly documented anywhere. Mostly I just want to understand this behavior so I can explain it when people ask questions like the one linked above.

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 con il TypeScript Playground collegato e confronta gli esempi di firme di indice implicite ed esplicita con le opzioni del compilatore mostrate. Segui il comportamento rilevante del controllo dei tipi per determinare la regola prevista, quindi documenta tale regola e la relazione con exactOptionalPropertyTypes e Partial; il lavoro è completato quando la discrepanza è spiegata chiaramente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Documentazione
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.