microsoft / microsoft/TypeScript

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

Offen
#59,392 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

In Discussion Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem verknüpften TypeScript Playground und vergleiche die Beispiele für implizite und explizite Index-Signaturen unter den angezeigten Compileroptionen. Verfolge das relevante Verhalten der Typprüfung, um die beabsichtigte Regel zu bestimmen, und dokumentiere anschließend diese Regel sowie den Zusammenhang mit exactOptionalPropertyTypes und Partial; als abgeschlossen gilt die Aufgabe, wenn die Abweichung eindeutig erklärt ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Dokumentation
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.