microsoft / microsoft/TypeScript
Type Confusion when overwriting field on object creation
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
Bug Report
🔎 Search Terms
- "type confusion" -union label:Bug
- object field -union label:Bug
... too many Bugs in this tracker to to more than skim the titles
🕗 Version & Regression Information
See in 4.9.5 after running into an example for this in a project.
The Playground also reproduces it in 3.3.3 and the 5.0.0 nightly.
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about object
⏯ Playground Link
[Playground link with relevant code](https://www.typescriptlang.org/play?ts=4.9.5#code/C4TwDgpgBAGlC8UDeUCGAuKBnYAnAlgHYDmANFAEaaECuAthRLlAL4BQbAxgPaE5SdcEVMGiIAFADN8EADYATAHKo6ETAGsIIbpNjlCEAO4BBavUa4AlJjjwAfMjZQoQ4DVyFkaTAHJUP8iooACZyAG1pOSUVCABdaiNjVjZ2Ll5+AA8EASERCHE-AJDLNL5uWQgAOlluYnFQSB0oDMrUSygAeg6oWgYmNiA
)
💻 Code
type X = { a: string, b: number }
const create = (fieldName: keyof X, newA: number): X => {
return { a: 'a', b: 2, [fieldName]: newA }
}
const x = create('a', 2)
console.log(typeof x.a) // number
🙁 Actual behavior
The code survives typechecks and has a number in x.a.
🙂 Expected behavior
TypeScript rejecting code that assigns a number to a field typed as string.
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
Das Issue stellt eine Reproduktion im TypeScript Playground bereit, nennt aber keine Datei oder keinen Test im Repository. Beginne damit, das Beispiel auszuführen und nachzuverfolgen, wie der Compiler die berechnete Eigenschaft im Objektliteral prüft; die Aufgabe ist erledigt, wenn TypeScript die dem String-Feld zugewiesene Zahl ablehnt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 30/100