microsoft / microsoft/TypeScript

Discriminated union with 11+ items does not handle nullability same as union with <10 items

Abierto
#62,511 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Bug Domain: check: Big Unions Help Wanted
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
2 d 4 h
PR fusionados (30 d)
132

Descripción

🔎 Search Terms

10 11 union undefined type
Nullability narrowing is inconsistent between type discriminated unions of <=10 items and >10 items

🕗 Version & Regression Information
  • This changed between versions 4.2.3 and 4.3.5 (before it would complain about undefined in both cases)
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.2#code/PTAEFUDsEsHtNAd2gFwBagIyYNSlQKYC2AzqACawFmSwqhoCGk5ANgaJAK6uuMBG0VqgCeoEoyIdGZLjHhJUGADyYADPhTESAKHIEAxnwBOHdvUYBmAFygA3qBQiADgVsAiTO9ABfUAB97Rxc3UHcAJm8-QLl9ADNoSAJyAG4daDiACisAQgA6J1dQAF5SsK8ASjsdUFrQEFAAcitG-DJnWBISaH5WMUbYggSk8ka8zMwADjUAFkmKmrqDeBJYdjzWWABzbMsCkIq0nx09QxMzAgt1WwdC0M8ogKC7j0jfJ9uQj0tHwM-XDwzX7PL5hACswP+9wAbJDggCwgB2OEvMKTFGg9wATgxCM8amBg2GyTSGWy6nydxKZQeVUWtQaADlYKACMZjLBjKBMvxLloubymAA3ODGAD8CzqoGWkFW602O0Y6n2rkOOh8QA

💻 Code
// Union with 11+ items does not handle nullability same as union with <10 items
declare let a3: { type: "1" } | { type: "2" } | undefined;
if(a3!.type === "1"){
    // 'a3' is possibly 'undefined'.(18048)
    console.log(a3.type);
}

declare let a10: { type: "1" } | { type: "2" } | { type: "3" } | { type: "4" } | { type: "5" } | { type: "6" } | { type: "7" } | { type: "8" } | { type: "9" } | { type: "10" } | undefined;
if(a10!.type === "1"){
    // No error (better behavior?)
    console.log(a10.type);
}
🙁 Actual behavior

Complains about undefined for unions with >10 members, but not for unions <11

🙂 Expected behavior

It seems like it should not error in either case - the code inside the if can only execute if the a3 variable is not undefined

Additional information about the issue

Relevant function in checker.ts: getKeyPropertyName(). That is where the 10 magic value comes from.

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 proporcionada en Playground e inspecciona checker.ts, especialmente getKeyPropertyName(), donde se identifica el umbral de 10 elementos. Compara el estrechamiento de nulabilidad para los dos tamaños de unión y considera el comportamiento esperado indicado en el issue: ningún ejemplo debería informar de undefined dentro del bloque protegido.

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
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.