microsoft / microsoft/TypeScript

Narrow length of tuples

Abierto
#33,182 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

In Discussion Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

Search Terms

tuple length narrow

Suggestion

Narrow the length property on tuple types when used as an Array.

Use Cases

Often times a method expects an array of some particular length. This can be expressed as [T, T, T] where the number of tuple items is the length of the array. However, sometimes the length is itself generic and in order to properly constrain the type you need to type the parameter as Array<T> & {length:L}. The problem with this is that if you have a tuple of length L, the compiler still assumes that the length is of type number.

It would be useful if a tuple type correctly narrowed the type of length to be the number of elements in the tuple. This way you could pass a tuple into a method that expects a fixed length array without casting.

Examples

declare function bytesToInt(bytes: Array<number> & {length:4}): number
bytesToInt([1,2,3,4]) // currently errors, would be nice if it didn't.
bytesToInt([1,2,3,4] as Array<number> & {length:4}) // error prone and something the compiler could do for you.

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.

It would be super cool if TypedArrays could get this behavior, since their length is fixed at construction time. I'm not sure exactly how to achieve this, so I'll leave it out of this suggestion but if it is possible for new Uint8Array(4) to result in a type Uint8Array & {length:4} that would be great.

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

El issue no nombra archivos, tests ni puntos de entrada. Empieza reproduciendo el ejemplo bytesToInt en un caso de prueba de TypeScript e investiga cómo se representa y se comprueba length de las tuplas. Se considera terminado cuando la llamada con la tupla se acepta sin un cast, mientras el comportamiento existente de las tuplas y los arrays permanece sin cambios.

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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.