- Error
- Reported by the type checker
TS2359
The right-hand side of an 'instanceof' expression must be either of type 'any', a class, function, or other type assignable to the 'Function' interface type, or an object type with a 'Symbol.hasInstance' method.
The right side of `instanceof` isn't a class or constructor. Use a class, or another kind of check.
What it means
x instanceof Y needs Y to be a constructor (or an object with Symbol.hasInstance). Interfaces and type aliases don't exist at runtime, and a plain value can't be used.
Example
const shape = { kind: "circle" };
export const ok = {} instanceof shape;
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(3,33): error TS2359: The right-hand side of an 'instanceof' expression must be either of type 'any', a class, function, or other type assignable to the 'Function' interface type, or an object type with a 'Symbol.hasInstance' method.
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 a class on the right.
- To test against an interface, check a discriminant or properties, or write a type guard.