microsoft / microsoft/TypeScript

Type is not checked when using object spread and computed property of type `number`

Offen
#43,698 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: check: Excess Property Checking
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Bug Report

🔎 Search Terms

spread computed property number reduce

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

Playground link with relevant code

💻 Code
declare const id: number;

type Acc = { [key: string]: number };
declare const acc: Acc;

// Expected error, but got none. ❌
const acc2: Acc = {
    ...acc,
    [id]: 'invalid',
};

If we change the type of the computed property id from number to string, it works as expected (we get an error).

declare const id: string;

type Acc = { [key: string]: number };
declare const acc: Acc;

// Expected error, and got none. ✅
const acc2: Acc = {
    ...acc,
    [id]: 'invalid',
};

Alternatively, if we remove the spread of acc it works as expected:

declare const id: number;

type Acc = { [key: string]: number };
// declare const acc: Acc;

// Expected error, and got none. ✅
const acc2: Acc = {
    [id]: 'invalid',
};

My workaround for now is to convert the type before it's used as a computed property:

declare const id: number;

type Acc = { [key: string]: number };
declare const acc: Acc;

// Expected error, and got none. ✅
const acc2: Acc = {
    ...acc,
    [id.toString()]: 'invalid',
};
🙁 Actual behavior

See code comments above.

🙂 Expected behavior

See code comments 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 der verlinkten TypeScript Playground-Reproduktion und vergleiche die Fälle mit einer numerischen beziehungsweise einer String-basierten berechneten Eigenschaft, jeweils mit und ohne den Object Spread. Verfolge das Type-Checking-Verhalten für das Objektliteral und überprüfe, dass der Fall mit der numerischen Eigenschaft einen Fehler meldet, während die anderen Beispiele ihr erwartetes Verhalten beibehalten.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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