• Error
  • Reported by the type checker

TS2358

The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.

The left side of `instanceof` is a primitive type, which is never an instance. Use `typeof` for primitives.

What it means

x instanceof C checks an object's prototype chain. When x is typed as a primitive such as string or number, the test can't succeed.

Example

index.ts
const value = "text";

export const isString = value instanceof String;
ts-r-check -p tsconfig.json --pretty false ts-r output
index.ts(3,25): error TS2358: The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.

Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.

How to fix

  • Use typeof value === "string" for primitives.