microsoft / microsoft/TypeScript
Infer a tuple type instead of array type when possible
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
Suggestion
I'd prefer typescript to be more aggressive about inferring tuple types, instead of always defaulting to Array.
This is related to https://github.com/microsoft/TypeScript/issues/6574. I'd like to open up a discussion about this again since that thread is locked, and conversation stopped after a workaround was provided without completely addressing the general issue.
🔍 Search Terms
Typescript, tuple, tuples, map, inference
✅ Viability 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, new syntax sugar for JS, etc.)
- [*] This feature would agree with the rest of TypeScript's Design Goals.
Existing typescript code could start emitting type errors from this change, so it would make sense to provide this as an option in tsconfig.
⭐ Suggestion
Typescript should infer a type to be a tuple instead of an array when it's possible.
📃 Motivating Example
The specific example I have in mind is for Array.prototype.map(), but there are probably more.
const tuple: [number, number, number] = [1, 2, 3];
const result = tuple.map(value => value + 1);
Here, typescript infers the type of result to be number[] rather than [number, number, number]. This could result in some wonkiness when trying to access values inside the tuple:
const [a, b, c, d] = result; // Typescript doesn't detect an error since result is an Array type.
const e = result[10]; // Typescript doesn't detect an error since result is an Array type.
Both of these errors can be caught if Typescript instead inferred result to be the tuple type [number, number, number].
💻 Use Cases
The above example is a use case. I want typescript to tell me when I make errors in my code.
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 revisando el comportamiento de inferencia de TypeScript para Array.prototype.map() y la discusión previa en el issue #6574. Define cuándo una tupla mapeada debe conservar su tipo de tupla, incluida la opción de tsconfig propuesta, y añade pruebas que muestren que el acceso no válido a la tupla se rechaza, mientras que los resultados mapeados válidos siguen funcionando.
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
- 25/100