-1

I would like to make simple dictionary in TS.

It's works but I getting error:

Element implicitly has an 'any' type because type '{ 'The ticket field is required.': string; 'The selected ticket is invalid.': string; }' has no index signature.

My code:

const translation = {
  'The ticket field is required.': 'Musisz podać numer swojego biletu.',
  'The selected ticket is invalid.': 'Wybrany bilet jest nieprawidłowy.'
};

this.error = translation['The ticket field is required.']; 
Sefe
  • 13,731
  • 5
  • 42
  • 55
John
  • 61
  • 2
  • 7
  • 1
    https://stackoverflow.com/questions/32968332/how-do-i-prevent-the-error-index-signature-of-object-type-implicitly-has-an-an – ellipsis Jan 28 '19 at 08:08
  • Possible duplicate of [How do I prevent the error "Index signature of object type implicitly has an 'any' type" when compiling typescript with noImplicitAny flag enabled?](https://stackoverflow.com/questions/32968332/how-do-i-prevent-the-error-index-signature-of-object-type-implicitly-has-an-an) – Slartibartfast Jan 28 '19 at 08:09

2 Answers2

2

When you declare the value translation like this:

const translation = {
  'The ticket field is required.': 'Musisz podać numer swojego biletu.',
  'The selected ticket is invalid.': 'Wybrany bilet jest nieprawidłowy.'
};

You are omitting the type declaration. That means translation is typed implicitly. If you would declare the respective type explicitly, your const declaration would look like this:

const translation:
{
    //This is the type declaration
    'The ticket field is required.': string;
    'The selected ticket is invalid.': string;
} = {
    //This is the value
    'The ticket field is required.': 'Musisz podać numer swojego biletu.',
    'The selected ticket is invalid.': 'Wybrany bilet jest nieprawidłowy.'
};

Since you have declared your value as const, the compiler knows that the only properties are the ones you have provided.

When you now are accessing your type like...

this.error = translation['The ticket field is required.'];

...you are using it as a mapped type. Since the compiler does not see (because it does not check) that the key you have provided matches one of the property names of the inferred type, it will assume that the type of the expression translation['The ticket field is required.'] is of type any. Since this is an implicit any and you are in strict mode, you get the error.

The simple solution is to provide explicit typing for your value:

const translation: { [key: string]: string } = {
    'The ticket field is required.': 'Musisz podać numer swojego biletu.',
    'The selected ticket is invalid.': 'Wybrany bilet jest nieprawidłowy.'
};
Sefe
  • 13,731
  • 5
  • 42
  • 55
1

Just try to provide a type hint:

const translation = {
  'The ticket field is required.': 'Musisz podać numer swojego biletu.',
  'The selected ticket is invalid.': 'Wybrany bilet jest nieprawidłowy.'
} as { [index:string] : string };
Karol Samborski
  • 2,757
  • 1
  • 11
  • 18
  • 1
    Instead of casting, you can have it as the type: `const translation: { [index:string] : string }` – smac89 Jan 28 '19 at 08:14