14

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:

private currentTask: string;

However, when I use the enums in [WORK, SLEEP].includes(currentTask) I get the following error.

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.

So what am I missing here?

1 Answer 1

8

currentTask is of type string, but WORK and SLEEP is enum members of type taskTypes

So [WORK, SLEEP] is an array of taskTypes enums. Such an array can never contain a string.

['work', 'sleep'] is an array of strings, which can contain a string.

If you type the private memeber to be of type taskTypes it will also work

private currentTask: taskTypes
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, I see, one question though, type taskTypes is an enum which is type string right?
@honeybadger_execute no, enums and strings are different types in TypeScript. "String enums" are a special case of enums, that "serialize" well (e.g. log), but still not the same as strings. typescriptlang.org/docs/handbook/enums.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.