microsoft / microsoft/TypeScript
Type narrowing down on a union type with null results in incorrect type when applying `typeof`
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 narrowing down, typeof
🕗 Version & Regression Information
I've tested all versions until 4.4.0 (Nightly)
⏯ Playground Link
💻 Code
type Container = { container: {type: "foo"} | {type: "bar"} | null }
function test(input: Container) {
if (input.container?.type === 'foo') {
// works
acceptsFoo(input.container);
acceptsFoo({type: "foo"})
// Does not works
acceptsFoo(input.container as typeof input.container)
acceptsFoo({type: "foo"} as typeof input.container)
/**
Error:
Argument of type '{ type: "foo"; } | null' is not assignable to parameter of type '{ type: "foo"; }'.
Type 'null' is not assignable to type '{ type: "foo"; }'.(2345) \
**/
}
}
function acceptsFoo(input: {type: "foo"}) {}
🙁 Actual behavior
input.container and input.container as typeof input.container are not equivalent.
🙂 Expected behavior
input.container and input.container as typeof input.container should be equivalent.
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
Reproduzca el comportamiento de narrowing reportado en el TypeScript Playground enlazado utilizando el Container y la función de prueba proporcionados. Empiece siguiendo el manejo del comprobador de tipos del narrowing de propiedades opcionales y las aserciones typeof; se considera terminado cuando input.container y input.container as typeof input.container se tratan de forma equivalente sin el error de nulabilidad.
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
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bien especificado
- Aptitud para principiantes
- 35/100