microsoft / microsoft/TypeScript
Narrowing empty string or false in generic misbehaves
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
🔎 Search Terms
generic, narrowing, empty string, false, falsy
🕗 Version & Regression Information
This is the behavior in every version I tried, but it seems to behave a little worse with TS v4.2.3 and prior.
⏯ Playground Link
💻 Code
function func1<T extends number | null | undefined | false | ''>(x: number | T) {
return x || undefined;
}
function func2<T extends number | null | undefined | false | ''>(x: number | T) {
return x ? x > 0 ? x : -x : x;
}
function func3<T extends number | null | undefined | false | ''>(x: number | T) {
return x ? Math.abs(x) : x;
}
const x: number | undefined = func1('');
const y = func2(-3);
const z = func3(-3);
🙁 Actual behavior
The inferred return type of func1 can contain false or '' depending on T. It's wrong because x || undefined can never result in false or '' and thus should not be in the resulting type.
func2 fails type checking: Operator '>' cannot be applied to types 'number | NonNullable<T>' and 'number'.. It's wrong because the truthiness check should narrow x to number. (Note that func3 succeeds type checking because the signature of Math.abs seems to provide supplemental information which narrows x to number.)
🙂 Expected behavior
The inferred return type of func1 is should number | undefined, regardless of T.
func2 is expected to pass type checking, x having been narrowed to number with the truthiness check.
Additional information about the issue
All functions act 'correctly' if defined without the use of generics, but it's inadequate because the return type of func2 and func3 would always be number | null | undefined | false | '' regardless of argument type.
My actual use case is creating functions that transform numeric inputs and propagate falsy values. I've worked around this issue using type assertions in spots where narrowing misbehaves.
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 con el ejemplo enlazado de TypeScript Playground y reproduce el comportamiento en func1, func2 y func3 usando las versiones del compilador indicadas. Rastrea la comprobación de tipos y el estrechamiento del flujo de control para parámetros genéricos bajo comprobaciones de veracidad; se considera terminado cuando func1 excluye false y '', mientras que func2 pasa la comprobación de tipos con x estrechado a number.
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
- 42/100