Enabling TypeScript "strict" mode with "noImplicitAny" causes this code to fail compilation.
I am looking for guidance on how to properly declare and use Arrays indexed by Enum values.
namespace CommandLineParser {
enum States { sNoWhere, sSwitchValue }
abstract class State {
}
class NoWhereState extends State {
}
class SwitchValueState extends State {
}
export class GetOption {
state: State;
states: Array<State>[States];
constructor() {
this.states = new Array(2);
this.states[States.sNoWhere] = new NoWhereState();
this.states[States.sSwitchValue] = new SwitchValueState();
this.state = this.states[States.sNoWhere];
}
}
}
let go = new CommandLineParser.GetOption();
The errors encountered are :
error TS7017: Element implicitly has an 'any' type because type 'State' has no index signature.
this.states[States.sNoWhere] = new NoWhereState(this);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error TS7017: Element implicitly has an 'any' type because type 'State' has no index signature.
this.states[States.sSwitchValue] = new SwitchValueState(this);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error TS7017: Element implicitly has an 'any' type because type 'State' has no index signature.
this.state = this.states[States.sNoWhere];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~