microsoft / microsoft/TypeScript

Type narrowing down on a union type with null results in incorrect type when applying `typeof`

Abierto
#44,914 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Bug Domain: check: Control Flow
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

https://www.typescriptlang.org/play?ts=4.4.0-dev.20210706#code/C4TwDgpgBAwg9gO2AQwJYIgJygXigbygGNEV0sAuA0SKgIgDM446BfKAH2vAnoCNkmNpygIArgBsJUVgFgAUAoZiERYKkRRgEAM7AAFOjBjgVeEjQZMASgIKoDqKgZRDCY8AB0JC+UwB+TxpoHFCoAHImOHDbfHtHBIB6RKgAdzhMAGsdeISHZCIiCDBgHQAxZjcPb1JLLGsAbly8gqKS8sr8YPootmtmpJSAEThdUThgNIzsgcdW4tKKuCqTGt8rKGQdLR44FyNVnzIrfvk8ucKFjuWunh7mYS2dyD2nd0Pav1OEuXlfpRUag0CE2l3aSxWpm4tCgjAerFirCAA

💻 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

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. 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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.