Type 'string' is not assignable to type: how to understand and fix the TypeScript error

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

«Type 'string' is not assignable to type» — what is it and why are you here?

\n\n

If you've ever written in TypeScript, you've likely encountered red underlining in the code editor and a message:

\n\n
Type 'string' is not assignable to type 'SomeType'.
\n\n

This error is a real «hello» from strict typing. It scares beginners, but in fact, it is the developer's best friend. In this article, we will break down why the Type 'string' is not assignable to type error occurs, how to fix it, and what approaches exist for working with string literals, union types, and generics. The article is written for the educational site pythonlib.ru, but the examples are entirely in TypeScript.

\n\n

1. The root of the problem: why does TypeScript complain about strings?

\n\n

TypeScript is a superset of JavaScript that adds static typing. When you write:

\n\n
let role: 'admin' | 'user' = 'admin';\nrole = 'moderator'; // Error: Type '"moderator"' is not assignable to type '"admin" | "user"'\n
\n\n

TypeScript understands that the variable role can only accept two values: 'admin' or 'user'. When you try to assign 'moderator', the compiler throws an error. But why does it say «Type 'string' is not assignable to type»?

\n\n

The thing is, the string literal 'moderator' has the type string, while the expected type is 'admin' | 'user'. TypeScript compares types, not values. It sees: «You're giving me a regular string, but I expect specific strings». Hence the message.

\n\n

1.1. Simple example for understanding

\n\n

Consider a basic case:

\n\n
type Status = 'active' | 'inactive';\n\nfunction updateStatus(status: Status) {\n  console.log(status);\n}\n\nconst myStatus = 'active';\nupdateStatus(myStatus); // OK\n\nconst anotherStatus = 'pending';\nupdateStatus(anotherStatus); // Error: Type 'string' is not assignable to type 'Status'\n
\n\n

In the last case, the variable anotherStatus is inferred as string, not as 'pending'. TypeScript doesn't know that 'pending' is a literal. It only sees the type string. You can fix this by explicitly specifying the type:

\n\n
const anotherStatus: 'pending' = 'pending'; // Now it works\n
\n\n

2. Approach comparison: how to work around or fix the error

\n\n

There are several ways to deal with the Type 'string' is not assignable to type error. We'll compare them by simplicity, safety, and performance.

\n\n

2.1. Approach 1: as const — freezing the literal

\n\n

The most elegant way is to use as const. It «freezes» the value as a literal:

\n\n
const myStatus = 'active' as const;\nupdateStatus(myStatus); // OK\n
\n\n

Pros: simple, readable, safe.
\nCons: works only with const, not with let.

\n\n

2.2. Approach 2: Type Assertion (as) — explicit casting

\n\n

You can tell TypeScript: «I know what I'm doing»:

\n\n
const someString: string = 'active';\nupdateStatus(someString as Status); // OK\n
\n\n

Pros: quick fix.
\nCons: you disable type checking. If you pass an invalid value, the error will only appear at runtime.

\n\n

2.3. Approach 3: Type Guard

\n\n

Let's write a function that checks whether a string is a valid status:

\n\n
function isStatus(value: string): value is Status {\n  return value === 'active' || value === 'inactive';\n}\n\nfunction safeUpdate(value: string) {\n  if (isStatus(value)) {\n    updateStatus(value); // TypeScript knows that value is Status\n  } else {\n    throw new Error('Invalid status');\n  }\n}\n

Blogs

Book Recommendations