我是TypeScript新手,我不明白需要做什么来修复生成TS7015错误的行(使用字符串变量引用枚举成员),因为紧跟其后的行不会出错(使用字符串文字引用枚举成员):
enum State {
Happy = 0,
Sad = 1,
Drunk = 2
}
function Emote(enumKey:string) {
console.log(State[enumKey]); // error TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'.
console.log(State["Happy"]); // no error
}
如果检测到错误,则在项目的tsconfig.json
中设置"noImplicitAny": true
"noImplictAny": false
设置在项目的tsconfig.json
中,未检测到任何错误
我用"ntypescript": "^1.201603060104.1"
来编译
我现在用"tsc": "1.8.10"
美元来编译
C:>npm install -g typescript
`-- typescript@1.8.10
验证安装:
C:\>tsc --version
Version 1.8.10
这是我的tsconfig.json
份文件:
{
"compileOnSave": true,
"compilerOptions": {
"target": "ES5",
"module": "System",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": true,
"noImplicitAny": true,
"sourceMap": true,
"mapRoot": "map/",
"diagnostics": true
},
"exclude": [
"node_modules",
"typings"
]
}
以下是编译器输出:
C:\>tsc
test.ts(8,17): error TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'.