Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Error Codes Worth Knowing

TypeScript error TS2352: conversion of type may be a mistake

TS2352 appears when a type assertion with as links two types that do not sufficiently overlap. See why it exists and the right alternatives to double assertions.

TS2352 reads 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. A type assertion (value as T) is allowed only when one of the two types is assignable to the other, in either direction. Asserting between unrelated types would be a lie the compiler can see through, so it refuses.

Assertions never change the value at runtime. They only tell the compiler to believe you. This error is a sanity check that your claim is at least plausible.

TypeScript
const n = "5" as number;   // error TS2352

Try it

TypeScript An impossible assertion

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Doing it properly

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

An assertion e as T from source type S is accepted when S is comparable to T, roughly "assignable in at least one direction".

Case Allowed?
S is a subtype of T (upcast), such as Dog as Animal Yes
T is a subtype of S (downcast), such as Animal as Dog Yes
string as number, {a: 1} as {b: 2} No: TS2352
x as unknown as T Yes: unknown is comparable to everything
x as any as T Yes, but gives up all checking

Usage notes

  • Prefer conversion to assertion. Number(s), String(n), parseInt, JSON.parse plus validation actually change or check the value.
  • Double assertions through unknown are an explicit "I know better" and show up in review. They are legitimate in tests and at FFI-style boundaries, and a red flag elsewhere.
  • Literals widen on purpose. "a" as "a" | "b" works because the literal type is assignable to the union.
  • as const is different. It is not a conversion at all; it narrows an expression to its most specific literal type. See as const.
  • Object literals: { x: 1 } as Point compiles even when properties are missing, because the literal is comparable to Point. This is a way to silence real errors; use satisfies to keep checking.
  • Angle-bracket assertions (<T>value) mean the same but are not allowed in .tsx files.