使用字符串类型参数访问枚举时出现 TypeScript TS7015 错误 [英] TypeScript TS7015 error when accessing an enum using a string type parameter
问题描述
我是 TypeScript 的新手,我不明白我需要做什么来修复产生 TS7015 错误的行(使用字符串变量引用枚举成员),因为紧随其后的行不会出错(引用一个使用字符串文字的枚举成员):
I am new to TypeScript and I don't understand what I need to do to fix the line that generates the TS7015 error (referencing an enum member using a string variable) because the line immediately following that does not error (referencing an enum member using a string literal):
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
中设置,检测到错误
"noImplicitAny": true
is set in the project's tsconfig.json
the error is detected
"noImplictAny": false
在项目的tsconfig.json
中设置,未检测到错误
"noImplictAny": false
is set in the project's tsconfig.json
no error is detected
我正在使用 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'.
推荐答案
如果您使用的是 TypeScript 2.1+,您可以将 enumKey
的类型更改为 keyof typeof State
,像这样:
If you're using TypeScript 2.1+, you can change enumKey
's type to keyof typeof State
, like this:
function Emote(enumKey: keyof typeof State) {...}
或者,如果函数的输入需要是 string
,则:
or, if the function's input is required to be a string
, this:
var state : State = State[enumKey as keyof typeof State];
说明:
因为enumKey
是一个任意的string
,TypeScript 不知道enumKey
是否是State<的一个成员的名字/code>,所以它会产生一个错误.TypeScript 2.1 引入了
keyof
运算符,它返回类型的已知公共属性名称的联合.使用 keyof
允许我们断言该属性确实在目标对象中.
Because enumKey
is an arbitrary string
, TypeScript doesn't know whether enumKey
is the name of a member of State
, so it generates an error. TypeScript 2.1 introduced the keyof
operator which returns a union of the known, public property names of a type. Using keyof
allows us to assert that the property is indeed in the target object.
但是,当您创建枚举时,TypeScript 实际上会同时生成 type(通常是 number
的子类型)和 value(您可以在表达式中引用的枚举对象).当您编写keyof State
时,您实际上将获得number
的文字属性名称的联合.要获取枚举对象的属性名称,您可以使用 keyof typeof State
.
However, when you create an enum, TypeScript actually produces both a type (which is typically a subtype of number
) and a value (the enum object that you can reference in expressions). When you write keyof State
, you're actually going to get a union of the literal property names of number
. To instead get the property names of the enum object, you can use keyof typeof State
.
来源:
https://github.com/Microsoft/TypeScript/issues/13775#issuecomment-276381229https://www.typescriptlang.org/docs/handbook/enums.html#enums-at-compile-time
这篇关于使用字符串类型参数访问枚举时出现 TypeScript TS7015 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!