Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom array index types #40648

Open
4 of 5 tasks
beethoven2493 opened this issue Sep 19, 2020 · 2 comments
Open
4 of 5 tasks

Custom array index types #40648

beethoven2493 opened this issue Sep 19, 2020 · 2 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@beethoven2493
Copy link

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.

@MartinJohns
Copy link
Contributor

Sounds like you want #37448.

@beethoven2493
Copy link
Author

@MartinJohns Well, that would solve it I guess? But I'm asking for something much more restricted than a general-purpose hashmap with array notation. This would be for specifically number-like keys. As such, I'd hope it would be much less controversial.

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Sep 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants