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 showFix it fast
Most common cause, 1 of 3A primitive is cast to a different primitive
Convert the value at runtime instead of asserting its type.
// before: const qty = input.value as number;
const qty = Number(input.value);
if (Number.isNaN(qty)) throw new Error("quantity must be a number");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.
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.
npx tsc --noEmit --pretty falseThen find the line you see in this list and open its fix:
- 1A primitive is cast to a different primitive
The message names two primitive types, such as 'string' and 'number', and no second line follows - 2An object is cast to an interface it does not match
A second line says Type 'A' is missing the following properties from type 'B' - 3A generic value is cast to an unrelated type parameter
The 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.
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.
// 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.
npx tsc --noEmitConfirmed by typescriptlang.org
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.
npx tsc --noEmit --pretty false | grep -A1 TS2352Fix
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.
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.
npx tsc --noEmit --strictConfirmed by typescriptlang.org
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.
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.
// 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.
npx tsc --noEmit --strictConfirmed by typescriptlang.org
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.