Switching on a template literal expression does not narrow the interpolated union variable
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 45/100
- Tipo de issue
- Error
- Claridad
- Bastante claro
- Estado de actividad
- Activo
- Stack tecnológico
- typescript
- Área
- compilers
Línea de trabajo
Comienza con la reproducción enlazada en TypeScript Playground y compara el comportamiento de template-literal switch y de igualdad con el switch directo sobre foo. Sigue la ruta existente de narrowing de union y template-literal del checker; el trabajo está terminado cuando foo se estreche al literal coincidente en cada caso sin hacer retroceder el control del switch directo.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
🔎 Search Terms
template literal, template expression, switch, narrowing, control flow analysis, interpolated variable, union, as const
🕗 Version & Regression Information
This is the behavior in every version tried (4.1.5, 5.9.2, 7.0.2; not expressible before 4.1), and I (had claude) review the FAQ for entries about type narrowing and template literal types
⏯ Playground Link
💻 Code
declare const foo: "apple" | "pear" | "banana";
switch (`${foo} pie` as const) {
case "apple pie":
const apple: "apple" = foo;
// ^? expected foo to narrow to "apple"; actually does not narrow
break;
}
// control: switching on foo directly narrows as expected
switch (foo) {
case "apple":
const apple: "apple" = foo;
break;
}
🙁 Actual behavior
Inside case "apple pie":, foo is still typed "apple" | "pear" | "banana", so const apple: "apple" = foo fails with:
error TS2322: Type '"apple" | "pear" | "banana"' is not assignable to type '"apple"'.
This is wrong because the case can only be reached when foo === "apple". The switch subject ${foo} pie as const is typed as "apple pie" | "pear pie" | "banana pie", and each case label corresponds to exactly one value of foo. The compiler already computes that correspondence, but control flow analysis doesn't propagate the match back to foo. The equivalent if ((${foo} pie as const) === "apple pie") fails the same way, while switching on foo directly narrows as expected.
🙂 Expected behavior
foo should narrow to "apple" inside case "apple pie", and correspondingly in the other cases.
Since foo's type is a finite union of string literals, ${foo} pie evaluates to a distinct literal for each member. The mapping from case label back to foo's value is unambiguous and already known to the checker, which is what makes the subject type "apple pie" | "pear pie" | "banana pie" in the first place.
Matching a case label should therefore narrow foo the same way switch (foo) does, analogous to how narrowing already works backwards through other expression forms like typeof foo === "string" and switch (true) (TS 5.3)
Additional information about the issue
I understand this may be a design limitation rather than a bug, since narrowing generally only tracks references and the switch subject here is an expression. But the checker already computes the exact one-to-one mapping from foo's members to the subject's members when it constructs the type "apple pie" | "pear pie" | "banana pie", so the information needed to narrow is present.
Related but distinct issues found while searching: #46045, #53887, #42007, #20949, #51426 (none cover narrowing the interpolated variable of a template literal expression used as a switch/equality subject).
This issue was drafted using the assistance of LLMs (claude), though everything was read, directed, and reviewed by me before submitting this issue through the github website.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 15 h
- PR fusionados (30 d)
- 106
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.
Más de microsoft/TypeScript
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
microsoft/TypeScript#64322 · 2 comentarios · 1 reacción · 2 asignados ·
-
Possible Improvement
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
microsoft/TypeScript#64278 · 1 comentario · 1 reacción ·
-
Docs
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
microsoft/TypeScript#64118 · 1 comentario ·
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 88/100
microsoft/TypeScript#64094 ·
-
Docs
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
microsoft/TypeScript#63959 · 5 comentarios ·
Todos los issues de microsoft/TypeScript
Issues similares
-
Type/Bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
OpenNSW/nsw-srilanka#497 ·
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 92/100
milvus-io/birdwatcher#545 ·
-
kind/bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
kubernetes-sigs/prow#953 · 1 comentario ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
caddyserver/caddy#8046 ·