简体   繁体   English

Typescript 枚举键入错误(“字符串”类型的参数不可分配给类型参数)

[英]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 类型,但WORKSLEEPtaskTypes类型的枚举成员

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.

相关问题 'string | 类型的参数 undefined' 不可分配给“string”类型的参数 - TypeScript 错误 - Argument of type 'string | undefined' is not assignable to parameter of type 'string' - TypeScript error Typescript 错误“类型参数不可分配给类型参数” - Typescript error 'argument of type is not assignable to parameter of type' Typescript object const 断言键入 -.includes() “'string' 类型的参数不可分配给 'never' 类型的参数。” - Typescript object const assertion typing - .includes() “Argument of type 'string' is not assignable to parameter of type 'never'.” Typescript - 'string | 类型的参数 (string | null)[]' 不能分配给“string”类型的参数 - Typescript - Argument of type 'string | (string | null)[]' is not assignable to parameter of type 'string' 'string | 类型的参数 undefined' 不能分配给'string' 类型的参数。 typescript '严格' - Argument of type 'string | undefined' is not assignable to parameter of type 'string'. typescript 'strict' Typescript:类型参数'string | undefined' 不能分配给“string”类型的参数 - Typescript: Argument of type 'string | undefined' is not assignable to parameter of type 'string' 'string' 类型的参数不可分配给 typeScript 中的 'never' 类型的参数 - Argument of type 'string' is not assignable to parameter of type 'never' in typeScript Angular2 / TypeScript:错误TS2345:类型'String'的参数不能分配给'string'类型的参数 - Angular2/TypeScript: error TS2345: Argument of type 'String' is not assignable to parameter of type 'string' 打字稿错误:“对象”类型的参数无法分配给“ {} []”类型的参数 - Typescript error: Argument of type 'Object' is not assignable to parameter of type '{}[]' React 组件上的 Typescript 错误:“元素”类型的参数不可分配给类型的参数 - Typescript error on React Component: Argument of type 'Element' is not assignable to parameter of type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM