microsoft / microsoft/TypeScript

Type Inference In Switch Statement Not Work With &&

Abierto
#38,580 3 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Bug Domain: check: Control Flow
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

TypeScript Version: 4.0.0-dev.20200512

Search Terms: switch, inference, switch and inference

Code

interface I1 {
    type: 'I1';
    value: string;
}

interface I2 {
    type: 'I2';
    value: number;
}

type T = I1 | I2;

let t: T | undefined;

if (t && t.type == 'I1') {
    t.value; // Type string
}

switch (t?.type) {
    case 'I1':
        t.value; // Type string
        break;
}

switch (t && t.type) {
    case 'I1':
        t.value; // Uh oh, error
        t!.value; // Uh oh, string | number
        break;
}

Expected behavior:
The compile should understand that t && t.type is like as t?.type except that has also other falsy values.

Actual behavior:
The compiler does not infer from such expression.

Playground Link: https://www.typescriptlang.org/play/?ts=4.0.0-dev.20200512#code/JYOwLgpgTgZghgYwgAgJIEZkG8BQz-JgCeADhAFzIDkGVA3HgQG5wA2ArhcgM5hSgBzBgF8cOUJFiIUqAEzZG+YmUo1Z9RchYcuIdgFsARtBFjlKACrIAvGkwAfNLIY5WEMIUpXH7EABMIGFAIPxdgGGQACg8AMhjCADpzG1sadCoASgUCRO1OOmQAekLkC1IUXn4QARxRHG4Ad2AwBAALKLAAfiTyrNwchDhuFDSqck0csAS8iALi0vKePkEJgkMoCDgAa1N6ppb26OQ4xPM+zUHh6lpxnMnptnyikoBVdoB7VoAaZGgod6gqyUAEIHjo5q8Pt8llUBMhHHojNAgch1psdrUcEA

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

Comienza con la reproducción vinculada en TypeScript Playground y compara el comportamiento de narrowing de las tres condiciones mostradas. Sigue el análisis de flujo de control del compilador para expresiones switch usando t \u0026\u0026 t.type; estará terminado cuando la rama final haga narrowing de t de forma coherente con el comportamiento esperado.

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
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.