【发布时间】:2016-07-18 21:34:30
【问题描述】:
我是 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
}
"noImplicitAny": true设置在项目的tsconfig.json中检测到错误
在项目的tsconfig.json中设置"noImplictAny": false未检测到错误
我正在编译 "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'.
【问题讨论】:
-
您在寻找什么样的答案?您已经注意到将
noImplicitAny设置为false会使错误消失... -
我不想关闭 noImplicitAny 检查,而是想修复代码以使其通过 noImplicitAny 检查。但更重要的是,我不明白为什么第一个引用错误,第二个引用通过(从类型上讲,两个枚举引用的类型是相同的......或者他们真的不是,我只是错过了一些东西(可能很明显) )
-
您使用的是什么版本的打字稿?我没有得到该代码的隐式任何类型错误。
-
"noImplicitAny": true,版本 1.8.2,没有错误,State[enumKey]
-
ntypescript看起来主要是为那些想直接使用 TypeScript 编译器 API 的人准备的。我想你想要typescript...
标签: typescript