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

Allow for indexable types other than string and number #19538

Closed
aryzing opened this issue Oct 27, 2017 · 2 comments
Closed

Allow for indexable types other than string and number #19538

aryzing opened this issue Oct 27, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@aryzing
Copy link

aryzing commented Oct 27, 2017

Given the following definition of a user and a user map,

type UserId = string;

interface User {
  id: UserId;
  name: string;
  email: string;
}

interface UserMap {
  [id: string]: User;
}

let userMap: UserMap = {
  id0001: {
    id: 'id0001',
    name: 'person1',
    email: '[email protected]',
  },
};

We can insert another user into the map by using a variable of type UserId,

let anotherUserId: UserId = 'id0002';

userMap[anotherUserId] = {
  id: anotherUserId,
  name: 'person2',
  email: '[email protected]',
}

However, I was surprised to find out that the indexable type can't be defined using the UserId type,

interface UserMap {
  [id: UserId]: User // error!
}

I'm sure we would all appreciate the certainty of knowing that the value we're using as an index is of the proper type, to avoid situations like

let productId: ProductId = 'prod123'

userMap[productId] = {/*...*/}

Is this a viable feature to include in TypeScript?

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 27, 2017
@RyanCavanaugh
Copy link
Member

See #7374 and its many linked duplicates

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants