Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

TypeScript rejects an as cast because the two types do not overlap

Your error

error TS2352: Conversion of type 'string' to type 'number' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.3 more lines it can show

Fix it fast

Most common cause, 1 of 3

A primitive is cast to a different primitive

Convert the value at runtime instead of asserting its type.

Fix
// before: const qty = input.value as number;
const qty = Number(input.value);
if (Number.isNaN(qty)) throw new Error("quantity must be a number");
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

TS2352, Conversion of type 'X' to type 'Y' may be a mistake, means an as assertion jumps between types that share nothing. An assertion never changes the value: convert primitives with Number(), narrow objects with a type guard, and give generic parameters a related constraint. Keep as unknown as for data you are sure of. Applies to TypeScript 7.0.

  • Applies to TypeScript 7.0.2 (tsc --strict), TypeScript handbook (current)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Compile once and read the indented line printed under each TS2352. Two primitive type names with no second line point to cause 1, a list of missing properties points to cause 2, and a sentence about 'T' being instantiated points to cause 3.

Run this first
npx tsc --noEmit --pretty false

Then find the line you see in this list and open its fix:

  1. 1A primitive is cast to a different primitiveThe message names two primitive types, such as 'string' and 'number', and no second line follows
  2. 2An object is cast to an interface it does not matchA second line says Type 'A' is missing the following properties from type 'B'
  3. 3A generic value is cast to an unrelated type parameterThe second line says 'T' could be instantiated with an arbitrary type, or with a different subtype of constraint

What does the error look like?

  • error TS2352: Conversion of type 'string' to type 'number' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
  • Type 'User' is missing the following properties from type 'Order': orderId, total
  • 'T' could be instantiated with an arbitrary type which could be unrelated to 'U'.
  • 'U' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'User'.

Cause 1 of 3

A primitive is cast to a different primitive

You seeThe message names two primitive types, such as 'string' and 'number', and no second line follows

Check

The TypeScript handbook says an assertion may only move to a more specific or less specific version of a type. string and number are not versions of each other, so the cast is refused. Assertions are also erased at compile time, so the value would stay a string.

Check
grep -rnE "as (number|string|boolean)\b" src/

Fix

Convert the value at runtime instead of asserting its type. Number() turns a numeric string into a number and returns NaN when it cannot, so check for that.

Fix
// before: const qty = input.value as number;
const qty = Number(input.value);
if (Number.isNaN(qty)) throw new Error("quantity must be a number");

Verify

The file compiles and qty holds a real number at runtime.

Verify
npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

Cause 2 of 3

An object is cast to an interface it does not match

You seeA second line says Type 'A' is missing the following properties from type 'B'

Check

Object types that do not overlap cannot be asserted into each other. The extra line lists the properties the target needs and the source lacks, which usually means the wrong variable or the wrong interface.

Check
npx tsc --noEmit --pretty false | grep -A1 TS2352

Fix

If the data really has the other shape (for example parsed JSON), type it as unknown and narrow it with a type predicate, so the check runs at runtime. If the shapes differ, build the target object from the source fields instead of casting.

Fix
function isOrder(x: unknown): x is Order {
  return typeof x === "object" && x !== null && "orderId" in x && "total" in x;
}

const data: unknown = JSON.parse(body);
if (isOrder(data)) {
  console.log(data.total);
}

Verify

Inside the if block TypeScript treats data as Order, with no cast and no TS2352.

Verify
npx tsc --noEmit --strict

Confirmed by typescriptlang.org

Did this fix it?

Cause 3 of 3

A generic value is cast to an unrelated type parameter

You seeThe second line says 'T' could be instantiated with an arbitrary type, or with a different subtype of constraint

Check

Inside a generic function TypeScript must allow for any type a caller might pass. Casting a value of type U to T fails when nothing ties T to U, and also when both only share a constraint, because a caller may pick two different subtypes.

Check
npx tsc --noEmit --pretty false | grep -B1 "could be instantiated"

Fix

Relate the parameters with a constraint, so T is declared as a subtype of U (or of the concrete source type). Only when the design cannot express that, cast through unknown and keep the cast in one small, tested helper.

Fix
// before: function pick<T, U>(u: U): T { return u as T; }
function pick<U, T extends U>(u: U): T { return u as T; }

// last resort, documented and isolated
function unsafeCast<T>(value: unknown): T { return value as T; }

Verify

tsc reports no TS2352 for the generic function, and call sites still type-check.

Verify
npx tsc --noEmit --strict

Confirmed by typescriptlang.org

Did this fix it?

How do I stop it coming back?

  • Make npx tsc --noEmit a required check for pull requests, so a fresh TS2352 blocks the merge instead of being silenced later.
  • Type every JSON.parse, fetch body and localStorage read as unknown and pass it through a type predicate or schema validator.
  • When a generic function needs a cast, write the constraint first; reach for as unknown as only with a comment saying why it is safe.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, QistLock, Taskvare.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.