microsoft / microsoft/TypeScript
Type Confusion when overwriting field on object creation
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
L’issue fornisce una riproduzione in TypeScript Playground, ma non indica alcun file o test del repository. Inizia eseguendo l’esempio e seguendo come il compilatore controlla la proprietà calcolata nel literal dell’oggetto; il lavoro è completato quando TypeScript rifiuta il numero assegnato al campo di tipo string.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 30/100