microsoft / microsoft/TypeScript

Custom array index types

Abierto
#40,648 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

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

Descripción

Search Terms

custom array index type

Suggestion

Arrays are indexed by integers, cool. But they could also be indexed by something equivalent to an integer, like an enum-like class or wrapper around a number. For example:

class PlayerNumber { constructor(readonly n: number); valueOf() { return this.n; } }
let currentTurn = 3;
const players: Player[];
let currentPlayer = new PlayerNumber(4);

// correct
players[+currentPlayer]
// oops, typo, but no protection
players[+currentTurn]

Imagine we had a TypedArray<K extends { valueOf(): number }, V> that in all respects was like Array, except all array indices in any function signature were of type K, and all arrays in any function signature were instead TypedArray<K, ...>. And the + wouldn't be necessary. Then:

// correct AND AWESOME
players[currentPlayer]
// compiler error
players[currentTurn]

Use Cases

I am writing a game framework in TypeScript and there are just lots of arrays everywhere indexed by players, token types, round number, etc and it's easy to mess up which index goes where.

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.

I'm unclear on the last bullet. There is a bit of "operator overloading" involved in the proposal as suggested. I am not sure if that is disqualifying and/or if there is an alternative that would work.

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

Empieza examinando el modelo TypedArray<K, V> de la propuesta y cómo TypeScript tipa actualmente la indexación de arrays y las expresiones basadas en valueOf. Determina si la seguridad de tipos solicitada puede encajar en las reglas existentes del sistema de tipos sin cambios en tiempo de ejecución; se considera terminado cuando exista un diseño concreto y aceptado, con el comportamiento correspondiente de comprobación de tipos y pruebas.

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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.