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

An index signature parameter type must be 'string' or 'number' - why not its's equivalent? #14934

Closed
m1gu3l opened this issue Mar 30, 2017 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@m1gu3l
Copy link
Contributor

m1gu3l commented Mar 30, 2017

TypeScript Version: 2.2.1

Code

type LimitedFieldName = "name" | "email" | "phone";

interface ObjectWithLimitedField {
	[key: LimitedFieldName]: string;
}

// not ok

type AnyFieldName = string;

interface ObjectWithAnyField {
	[key: AnyFieldName]: string;
}

// not ok

interface ObjectWithStringField {
	[key: string]: string;
}

// ok

Expected behavior:
Index signature should allow types that evaluate to string or subset of string. The same for number.

Actual behavior:
Any literal other than string and number is explicitly disallowed.

@LOZORD
Copy link

LOZORD commented Mar 30, 2017

For your first example, see Mapped Types.

type Keys = 'option1' | 'option2';
type Flags = { [K in Keys]: boolean };

WRT using string type aliases as the index type for interfaces, etc., I believe this is being worked on, but I don't remember which issue is tracking it.

@RyanCavanaugh
Copy link
Member

See #5683

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

Or #7374

@m1gu3l
Copy link
Contributor Author

m1gu3l commented Mar 31, 2017

@LOZORD , thanks, I must have missed that.
@RyanCavanaugh, sorry, closing.

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