Skip to content

Type 'string' is not assignable to type 'number'

Error message

                Type 'string' is not assignable to type 'number'
              

Quick summary

You tried to assign a value of one type to a variable or parameter of a different, incompatible type. TypeScript caught the mismatch at compile time.

Why this happens

  • Assigning a string to a number-typed variable (or vice versa)

  • Returning the wrong type from a function

  • Passing the wrong type as a function argument

  • A type annotation that doesn't match the actual value

Minimal example

✗ Broken code
let count: number = 10;
count = "twenty";
✓ Fixed code
let count: number = 10;
count = 20;

count is typed as number, so assign a number value, not a string.

How to diagnose

  • Check the type annotation on the variable/parameter against the value being assigned

  • Use typeof to check the runtime type if unsure

  • Look at the error message — it tells you both the source type and target type

How to fix

  • Change the type annotation to match the value (e.g., : string instead of : number)

  • Change the value to match the type (e.g., remove quotes from a number)

  • Convert the value to the correct type (e.g., Number(str) or String(num))

How to prevent

  • Enable strict mode to catch type mismatches early

  • Use type inference where possible to avoid annotation mistakes

  • Write unit tests that verify type correctness

Related Resources

← Back to language