I must not be understanding how type syntax actually works in typescript... I have the following class:
class MyService {
static _current_state = 'FOO';
private static readonly STATE_MAPPING: { [ string: number ] : number } = {
'FOO': 123,
'BAR': 456,
};
static get STATE () : string {
return MyService._current_state;
}
...etc
private isValidThreshold (value: number) : boolean {
return MyService.STATE_MAPPING[LogService.STATE] >= value;
}
...etc
I get error TS2322: Object literal may only specify known properties, and ''FOO'' does not exist in type '{ [string: number]: number; }'.
and error TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'.
What am I doing wrong?
CodePudding user response:
TS clearly states that you don't have FOO prop in STATE_MAPPING and it has only indexer with a number as a key and FOO is definitely not a number.
class MyService {
static _current_state = 'FOO';
private static readonly STATE_MAPPING: { [ index: string ] : number } = {
'FOO': 123,
'BAR': 456,
};
static get STATE () : string {
return MyService._current_state;
}
}
You can play with it here
