【发布时间】:2019-03-07 11:34:41
【问题描述】:
由于“noImplicitAny”,启用 TypeScript“严格”模式时,此代码无法编译。
你能告诉我如何声明/使用由枚举值索引的数组吗?
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();
错误是:
错误 TS7017:元素隐式具有“任何”类型,因为“状态”类型没有索引签名。
this.states[States.sNoWhere] = new NoWhereState(this);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
错误 TS7017:元素隐式具有“任何”类型,因为“状态”类型没有索引签名。
this.states[States.sSwitchValue] = new SwitchValueState(this);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
错误 TS7017:元素隐式具有“任何”类型,因为“状态”类型没有索引签名。
this.state = this.states[States.sNoWhere];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
【问题讨论】:
-
你想要一个数组有什么原因吗?一个对象会更好地工作
标签: typescript