Type string is not assignable to type Enum in TypeScript

avatar

Borislav Hadzhiev

Last updated: Mar 6, 2022

banner

Photo from Unsplash

Type string is not assignable to type Enum in TypeScript #

The error "Type string is not assignable to type Enum" occurs when we try to use a string literal in the place of an enum value. To solve the error, use dot or bracket notation to access the specific enum property or use a type assertion.

Here is an example of how the error occurs.

index.ts
export enum EmailStatus { Read = 'READ', Unread = 'UNREAD', Draft = 'DRAFT', } // ⛔️ Type '"READ"' is not assignable // to type 'EmailStatus'.ts(2322) const status: EmailStatus = 'READ';

TypeScript is telling us that we are trying to assign a specific string value to a variable that has a type of EmailStatus.

To solve the error, access the specific property on the enum using dot or bracket notation.

index.ts
export enum EmailStatus { Read = 'READ', Unread = 'UNREAD', Draft = 'DRAFT', } // 👇️ using dot notation const status: EmailStatus = EmailStatus.Read; console.log(status); // 👉️ "READ"

Enums are real objects that exist at runtime.

index.ts
export enum EmailStatus { Read = 'READ', Unread = 'UNREAD', Draft = 'DRAFT', } // 👇️ using bracket notation const status: EmailStatus = EmailStatus['Read']; console.log(status); // 👉️ "READ"

You might have the specific property of the enum stored in a variable.

index.ts
export enum EmailStatus { Read = 'READ', Unread = 'UNREAD', Draft = 'DRAFT', } const str = 'Read'; // 👇️ get value with variable property const status: EmailStatus = EmailStatus[str]; console.log(status); // 👉️ "READ"

Just pass the variable between the square brackets to get the corresponding value.

If, for some reason, you can't access a property on the enum, you can use a type assertion.

index.ts
export enum EmailStatus { Read = 'READ', Unread = 'UNREAD', Draft = 'DRAFT', } // 👇️ const status: EmailStatus const status: EmailStatus = 'READ' as EmailStatus; console.log(status); // 👉️ "READ"

Type assertions are used when we have information about the type of a value that TypeScript can't know about.

When we use the type assertion, we basically tell TypeScript, the status variable is going to be of type EmailStatus, so just type it as that.

However, when using this approach you wouldn't get a type checking error if you misspell the string in the assignment.

index.ts
export enum EmailStatus { Read = 'READ', Unread = 'UNREAD', Draft = 'DRAFT', } // 👇️ const status: EmailStatus const status: EmailStatus = 'RED' as EmailStatus; console.log(status); // 👉️ "RED"

The status variable still has a type of EmailStatus, even though we misspelled the string literal upon assignment.

Enums are real objects that exist in runtime. The correct way to use them is to access their properties via dot or bracket notation.

Trying to use hardcoded strings with enums is a common cause of the error.

Conclusion #

The error "Type string is not assignable to type Enum" occurs when we try to use a string literal in the place of an enum value. To solve the error, use dot or bracket notation to access the specific enum property or use a type assertion.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.