microsoft / microsoft/TypeScript
Narrow number literal types with comparison
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
Search Terms
constant number literals narrowing comparison
Suggestion
Narrow number literal type not only with (non)equality, but also with less-than etc.
This request is only about throwing away non-matching literals, and not about full-blown number narrowing.
Use Cases
This could be used to direct narrowing with tuple unions.
Examples
With literals
type X = 0 | 1 | 2 | 3;
function f(): X {
return 1;
}
function g(x: 0 | 3) {
console.log(x);
}
let x: X = f();
g(x); // Error, 1 | 2 is unexpected
if (x > 2) {
g(x); // Same error, but should be safe
}
if (x != 1 && x != 2) {
g(x); // Ok
}
With tuples
type X = [] | [string, string];
function f(): X { return [];}
function g(x: string) {
console.log(x);
}
let x: X = f();
g(x[0]); // Error, undefined is unexpected
if (x.length > 0) {
g(x[0]); // Error, but should be ok
}
if (x.length == 2) {
g(x[0]); // Ok
}
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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 los ejemplos de literales y tuplas del issue, reproduciendo sus diagnósticos actuales y su comportamiento de narrowing. Define los resultados esperados del flujo de control para las comprobaciones de comparación, incluidas las comparaciones menor que y de longitud de tuplas, y verifica que el cambio solo elimine casos de literales imposibles sin introducir un narrowing general de números.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100