Andrew's Digital Garden

String is not assignable to type

When using [[string literal types]], its common to run into the following error:

Type 'string' is not assignable to type

Usually the code looks something like this:

type EmailStatus = 'draft' | 'read' | 'unread'; // 👇️ status has type `string` let status = 'draft'; status = 'read'; // ⛔️ Error: Type 'string' is not // assignable to type 'EmailStatus'.ts(2322) const current: EmailStatus = status;

This is because status is using the broader type of string, rather than the narrower type of EmailStatus.

One possible solution is to cast the value to the type:

let status = 'draft' as EmailStatus;

The other solution is to narrow the type with a [[const assertion]]:

let status = 'draft' as const;

[[20210705102809-ts-narrowing]]

[[ts]]

String is not assignable to type