hon*_*ute 6 javascript typescript
我有一个枚举定义如下:
export enum taskTypes {
WORK = 'work',
SLEEP = 'sleep',
EXERCISE = 'exercise',
EAT = 'eat'
}
Run Code Online (Sandbox Code Playgroud)
在我的班级的顶部,我定义currentTask如下:
private currentTask: string;
Run Code Online (Sandbox Code Playgroud)
但是,当我在中使用枚举时,[WORK, SLEEP].includes(currentTask)出现以下错误。
Argument of type 'string' is not assignable to parameter of type 'currentTask'
奇怪的是,当我只是将它更改为使用实际字符串时,它不会抱怨。
['work', 'sleep'].includes(currentTask) ===> 这有效。
那么我在这里错过了什么?
currentTask是字符串类型,但WORKandSLEEP是类型的枚举成员taskTypes
[WORK, SLEEP]枚举数组也是如此taskTypes。这样的数组永远不能包含字符串。
['work', 'sleep']是一个字符串数组,其中可以包含一个字符串。
如果您输入私人会员类型,taskTypes它也可以工作
private currentTask: taskTypes
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5002 次 |
| 最近记录: |