[英]Typescript enums typing error (argument of type 'string' is not assignable to parameter of type)
I have an enum define as follows:我有一个枚举定义如下:
export enum taskTypes {
WORK = 'work',
SLEEP = 'sleep',
EXERCISE = 'exercise',
EAT = 'eat'
}
On the top of my class I define the currentTask
as follows:在我的 class 的顶部,我将
currentTask
定义如下:
private currentTask: string;
However, when I use the enums in [WORK, SLEEP].includes(currentTask)
I get the following error.但是,当我在
[WORK, SLEEP].includes(currentTask)
中使用枚举时,出现以下错误。
Argument of type 'string' is not assignable to parameter of type 'currentTask'
The weird thing is when I simply just change it to use the actual string it doesn't complain.奇怪的是,当我只是将其更改为使用它不会抱怨的实际字符串时。
['work', 'sleep'].includes(currentTask)
===> this works. ['work', 'sleep'].includes(currentTask)
===> 这行得通。
So what am I missing here?那么我在这里错过了什么?
currentTask
is of type string, but WORK
and SLEEP
is enum members of type taskTypes
currentTask
是 string 类型,但WORK
和SLEEP
是taskTypes
类型的枚举成员
So [WORK, SLEEP]
is an array of taskTypes
enums.所以
[WORK, SLEEP]
是一个taskTypes
枚举数组。 Such an array can never contain a string.这样的数组永远不能包含字符串。
['work', 'sleep']
is an array of strings, which can contain a string. ['work', 'sleep']
是一个字符串数组,可以包含一个字符串。
If you type the private memeber to be of type taskTypes
it will also work如果您将私有成员键入为
taskTypes
类型,它也将起作用
private currentTask: taskTypes
Hi My case is similar and we are using strict mode in tsconfig,您好我的情况类似,我们在 tsconfig 中使用严格模式,
--> my emun --> 我的埃蒙
export enum User {
IMPLANTING = 'Imp',
TREATING = 'Treate',
CONSULTING = 'Guest'
}
In component Im importing this User emnum and trying to test the roles array在组件中,我正在导入此用户 emnum 并尝试测试角色数组
import {User} from './common.enum'
this.userRoles = User
let test = userRoles.includes(this.userRoles.IMPLANTING);
// here im getting error as // 这里我得到错误
argument of type 'any' is not assignable to parameter of type 'never'.
“any”类型的参数不能分配给“never”类型的参数。
Can some one help有人可以帮忙吗
@KlasMellbourn #KlasMellbourn @KlasMellbourn #KlasMellbourn
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.