microsoft / microsoft/TypeScript
Type Confusion when overwriting field on object creation
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
El issue proporciona una reproducción en TypeScript Playground, pero no menciona ningún archivo ni prueba del repositorio. Empieza ejecutando el ejemplo y siguiendo cómo el compilador comprueba la propiedad calculada en el literal de objeto; está hecho cuando TypeScript rechaza el número asignado al campo de tipo string.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bien especificado
- Aptitud para principiantes
- 30/100