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.'
};