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

{ [key in keyof typeof Enum]: any } is not index signature #20011

Closed
CzBuCHi opened this issue Nov 14, 2017 · 8 comments
Closed

{ [key in keyof typeof Enum]: any } is not index signature #20011

CzBuCHi opened this issue Nov 14, 2017 · 8 comments
Labels
Fixed A PR has been merged for this issue

Comments

@CzBuCHi
Copy link

CzBuCHi commented Nov 14, 2017

TypeScript Version: 2.4.1

Code

enum Name {
  Fred, Vilma
}
type Names = keyof typeof Name; // type Names = "Fred" | "Vilma";
type Data = ...;

const data: { [name in Names]: Data } = {
  Fred: { ... },
  Vilma: { ... },
}

function getData(name: Names) {
  return data[strName]; // error here
}

Expected behavior:
No error
Actual behavior:
Compilation error message:
Element implicitly has an 'any' type because type '{ readonly Fred: Data; readonly Vilma: Data; ...' has no index signature.

can be fixed by casting data to any or to { [name: string]: Data } before using indexer

@mhegazy
Copy link
Contributor

mhegazy commented Nov 14, 2017

Should be fixed in TS 2.6 (playground link).

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Nov 14, 2017
@mhegazy mhegazy added this to the TypeScript 2.6 milestone Nov 14, 2017
@mhegazy mhegazy closed this as completed Nov 14, 2017
@CzBuCHi
Copy link
Author

CzBuCHi commented Nov 14, 2017

wow that was fast ... nes time i will try update ts first 👍

@CzBuCHi
Copy link
Author

CzBuCHi commented Nov 16, 2017

updated to 2.6.1 and error still persist. This is my real code:

enum State {
  Fail, Inconclusive, Running, Success,
}
type StateKeys = keyof typeof State;
type States = {
  [state in StateKeys]: string[]
};
const test: States = getStatesSomehow();
const state = State.Fail;

// Element implicitly has an 'any' type because type 'States' has no index signature.
test[state].push('something');
~~~~~~~~~~~

@mhegazy
Copy link
Contributor

mhegazy commented Nov 16, 2017

``ts
const state = State.Fail;

state is a number, but test has keys that are strings.. did you mean to use string enums?

@CzBuCHi
Copy link
Author

CzBuCHi commented Nov 16, 2017

yea .... silly me :) ... btw: could be that error message tweaked little - basically print your response?

@mhegazy
Copy link
Contributor

mhegazy commented Nov 17, 2017

btw: could be that error message tweaked little - basically print your response?

if you want to take a stab at making the error better and send us a PR, would be happy to review it.

@chriszrc
Copy link

How would one use this type of key, but then use the enum value instead? As in :

enum Name {
  Fred = "fred",
  Vilma = "vilma"
}

...

test["fred"].push('something');

@CzBuCHi
Copy link
Author

CzBuCHi commented Apr 3, 2018

@chriszrc i would use it this way:

test[Name.Fred].push('something');

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants