我是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
}
Run Code Online (Sandbox Code Playgroud)
"noImplicitAny": true在项目中设置tsconfig.json检测到错误
"noImplictAny": false在项目中设置tsconfig.json没有检测到错误
我正在编译 "ntypescript": "^1.201603060104.1"
我正在编译 "tsc": "1.8.10"
C:>npm install -g typescript
`-- typescript@1.8.10
Run Code Online (Sandbox Code Playgroud)
验证安装:
C:\>tsc --version
Version 1.8.10
Run Code Online (Sandbox Code Playgroud)
这是我的tsconfig.json档案:
{
"compileOnSave": true,
"compilerOptions": {
"target": "ES5",
"module": "System",
"moduleResolution": …Run Code Online (Sandbox Code Playgroud) 当我尝试使用字符串Enum Localization变量访问内部的值时,出现此错误。locale
enum Localization {
'en-US' = '.com',
'pt-BR' = '.com.br',
'en-CA' = '.com.ca',
'en-AU' = '.com.au',
'en-IE' = '.com.ie',
'string' = 'string'
};
Run Code Online (Sandbox Code Playgroud)
const locale:string = 'pt-BR' //This value will come from DB.
const result = Localization[locale];
Run Code Online (Sandbox Code Playgroud)
错误:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof Localization'. No index signature with a parameter of type 'string' was found on type 'typeof Localization'.
在 Javascript 中工作正常。 …