microsoft / microsoft/TypeScript
union of tuples + spread tricks control flow analysis into thinking a reachable branch is not reachable
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
tuple spread control flow analysis type narrowing
🕗 Version & Regression Information
- This exists since at least 4.0.5 and is still present in the current 4.4.0-beta.
- 3.9.7 correctly fails to compile the snippet, but for what appear to be different reasons.
⏯ Playground Link
let current:
| []
| [string]
| [string, string] = [];
const values = ['foo', 'bar', 'baz'];
for (const v of values) {
if (current.length === 0) {
current = ['first'];
} else if (current.length === 1) {
current; // -> correctly narrows type to [string]
current = [...current, 'second'];
current; // -> corrently infers type [string, string]
// if, instead, we construct the tuple without spreads, the compiler correctly realizes the case below is reachable
// current = [current[0], 'second'];
} else {
assertNever(current); // this should not compile; because this case is actually reachable
console.log('here!'); // this line is hit, as expected
}
}
function assertNever(condition: never) {
}
Output
"use strict";
let current = [];
const values = ['foo', 'bar', 'baz'];
for (const v of values) {
if (current.length === 0) {
current = ['first'];
}
else if (current.length === 1) {
current; // -> correctly narrows type to [string]
current = [...current, 'second'];
current; // -> corrently infers type [string, string]
// if, instead, we construct the tuple without spreads, the compiler correctly realizes the case below is reachable
// current = [current[0], 'second'];
}
else {
assertNever(current); // this should not compile; because this case is actually reachable
console.log('here!'); // this line is hit, as expected
}
}
function assertNever(condition) {
}
Compiler Options
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"target": "ES2017",
"jsx": "react",
"module": "ESNext",
"moduleResolution": "node"
}
}
Playground Link: Provided
🙁 Actual behavior
The compiler erroneously claims the last case where current is length 2 cannot be reached (as represented by assertNever compiling without error) when current is reassigned with tuple spread. Furthermore, the compiler correctly identifies that current is a 2-tuple after the spread statement. The declared types also specify that current can be a 2-tuple, which leads me to believe it's the control flow analyzer getting fooled into narrowing the types too far.
The compiler correctly claims the last case can be reached when current is reassigned with each tuple member explicitly stated, which is equivalent to the spread in this case.
🙂 Expected behavior
The usage of spread/explicitly specifying tuple members should both fail to compile.
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
Comienza reproduciendo el ejemplo proporcionado de propagación de tuplas en TypeScript Playground con las opciones estrictas del compilador indicadas. Sigue el análisis del flujo de control alrededor de las comprobaciones de longitud y la reasignación de la propagación. Se considera terminado cuando tanto las asignaciones mediante propagación como las asignaciones de tuplas construidas explícitamente rechazan correctamente la rama alcanzable de assertNever.
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
- 42/100