microsoft / microsoft/TypeScript
Custom array index types
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia esaminando il modello TypedArray<K, V> della proposta e il modo in cui TypeScript tipizza attualmente l’indicizzazione degli array e le espressioni basate su valueOf. Determina se la sicurezza dei tipi richiesta può rientrare nelle regole esistenti del sistema dei tipi senza modifiche a runtime; il lavoro è completo quando esiste un design concreto e accettato, con il comportamento corrispondente del controllo dei tipi e i test.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100