Skip to main content

TypeScript · 6 common causes

TS2345: Argument is not assignable

Updated

The value you passed has a type the function does not accept.

The exact message

src/cart.ts:14:18 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.

14   applyDiscount(input.value);
                   ~~~~~~~~~~~

  src/cart.ts:3:24
    3 function applyDiscount(percent: number) {
                             ~~~~~~~~~~~~~~~
    The expected type comes from parameter 'percent'

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is TS2345: Argument is not assignable.

How to fix it

Cause 1 of 6

The value genuinely is the wrong type.

Convert it at the boundary where it enters your code: Number(input.value), then check the result with Number.isNaN before using it. Reading the full message matters — the part after 'is not assignable' names the exact incompatible piece, which for object types is a specific property.

Cause 2 of 6

The value may be undefined or null.

string | undefined is not assignable to string. Narrow it first with a guard — if (value === undefined) return; — and TypeScript will accept it for the rest of the block. Under strictNullChecks this is the most common form of TS2345.

Cause 3 of 6

The argument is wider than the parameter's union.

string is not assignable to 'small' | 'large'. Narrow with a check, or keep the source narrow: as const on a literal or an object literal preserves the literal types instead of widening them to string.

Cause 4 of 6

An object literal has a missing or excess property.

The message points at the property name. Add the missing one; for an excess one, either widen the parameter type or drop the property — excess property checking only fires on fresh object literals, which is why assigning through a variable first appears to 'fix' it without fixing anything.

Cause 5 of 6

Two structurally identical types that are not the same type.

A message reading 'Type X is not assignable to type X' means there are two copies of the declaration — usually duplicate installs of the same @types package. npm ls @types/<name> shows the duplicates; deduping or aligning versions removes it.

Cause 6 of 6

You are about to reach for a cast.

value as number silences the compiler without changing the value, so the failure just moves to runtime. Use it only for something you know and the compiler cannot, and prefer a type guard function that actually checks.

Stopping it coming back

Parse and convert external data once, at the edge — a Zod or Valibot schema returns properly typed values — so the rest of the codebase never has to reconcile a string with a number.

TS2345: Argument is not assignable — common questions

How do I read a long TS2345 about nested objects?
Read it bottom-up. The last lines are the most specific — they name the exact property and the primitive types that clash. The top line is only the outermost shape.
Why does assigning to a variable first make the error disappear?
Excess property checking applies to object literals passed directly. Through a variable, TypeScript uses ordinary structural compatibility, so the extra property is allowed. The mismatch is still there — you have only stopped it being reported.
Is as unknown as T ever acceptable?
Only at a boundary where you are asserting something you have verified another way, with a comment saying how. It defeats every check between the two types, so it should be rare enough to notice in review.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor