Typescript is a superset of javascript and ultimately compiles down to javascript. That being said it's important to think about what the code you are writing will look like as javascript. Here is what this will look like compiled:

const myFunc = value => {
  if (value) return "a string";
  return {
    id: 12,
    name: "model"
  };
};
const a = myFunc(true);
if (a instanceof string) {
  console.log("it is a string");
}
if (a instanceof IMyModel) {
  console.log("it is a model");
}

You'll notice that the types you declared are compiled away. The error is starting to make a bit more sense now. We can't call instanceof on a typescript type because they don't exist as far as the compiled javascript is concerned. You are probably looking for either:

typeof type guards

if (typeof a === 'string') {
  console.log("it is a string");
}

User defined type guards:

const isMyModel = (model: any): model is IMyModel => {
  return typeof model === 'object' && Boolean(model.id && model.name)
}
if (isMyModel(a)) {
  console.log("it is a model");
}

or both!

Answer from Donovan Hiland on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › instanceof
instanceof - JavaScript - MDN Web Docs
July 8, 2025 - C.prototype = {}; const o2 = new C(); o2 instanceof C; // true // false, because C.prototype is nowhere in // o's prototype chain anymore o instanceof C; D.prototype = new C(); // add C to [[Prototype]] linkage of D const o3 = new D(); o3 instanceof D; // true o3 instanceof C; // true since C.prototype is now in o3's prototype chain
🌐
GeeksforGeeks
geeksforgeeks.org › typescript › typescript-instanceof-operator
TypeScript Instanceof Operator - GeeksforGeeks
July 22, 2024 - The instanceof operator is a powerful tool in TypeScript for type checking at runtime. It allows you to verify if an object is an instance of a specific class or constructor function, which can be useful in various scenarios such as type assertions ...
Discussions

Using instanceof in TypeScript to find return type of function results in "refers to a type, but is being used as a value here" - Stack Overflow
I am using TypeScript and have a situation where a function returns a union of two types. It can either return a string or a model but I don't know how to get the type and perform an action based o... More on stackoverflow.com
🌐 stackoverflow.com
If typescript is duck-typed, how come when you use "instanceof", typescript accurately separate out the individual classes that implement the same interface? And how can we take advantage of this functionality in our typing?
instanceof tests *at runtime*, not at compile time, and it tests towards the *class* Error, and not the *interface* Error. Both exists and are defined in TS. This kind of situations is a bit of a mess, because a class is both a type at build time and a value at runtime (classes are just functions). More on reddit.com
🌐 r/typescript
27
0
October 23, 2024
Can instanceOf operator be used to check if an object is an instance of a n interface, or type?
To add to what others have said already, there's a neat trick you can do in typescript to check if your instance is of a given "type". You declare it as an abstract class and implement the Symbol.hasInstance method. Example: abstract class Person { abstract readonly name: string; abstract readonly age: number; public static [Symbol.hasInstance](instance: unknown): instance is Person { if (instance === null || (typeof instance !== 'object' && !(instance instanceof Person))) return false; const name: unknown = (instance as any).name; const age: unknown = (instance as any).age; if (typeof name !== 'string' && !(name instanceof String)) return false; return typeof age === 'number' || age instanceof Number; } } Then you can use it as: const person = { name: "SomeName", age: 55 }; const noPerson = { weight: "300kg" }; console.log(person instanceof Person); // will print true console.log(noPerson instanceof Person); // will print false The advantage of this method in my opinion is that because typescript has a structure typing system, you can just do: class PersonImpl implements Person { constructor( readonly name: string, readonly age: number ) { } } console.log(new PersonImpl("SomeName", 55) instanceof Person); // will also print true This way, if you make a typo on PersonImpl you will know immediately that it's wrong and at runtime you can still type check. More on reddit.com
🌐 r/typescript
10
5
August 10, 2023
Proposal: Type-side use of `instanceof` keyword as an instance-query to allow instanceof checking
🔍 Search Terms type side instanceof keyword used for instanceof Querying. related issues: #202 #31311 #42534 #50714 #55032 ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't chan... More on github.com
🌐 github.com
7
April 13, 2024
🌐
Learn TypeScript
learntypescript.dev › 07 › l4-instanceof-type-guard
Using an `instanceof` type guard | Learn TypeScript
instanceof is a JavaScript operator that can check whether an object belongs to a particular class. It also takes inheritance into account. ... The expression returns true or false depending on whether the object belongs to the class or not.
🌐
Ultimate Courses
ultimatecourses.com › blog › understanding-typescript-instanceof-type-guard
Understanding TypeScript: instanceof Type Guard - Ultimate Courses
That’s an overview of the instanceof, how it works, the prototype and how we can use them to infer types with our Type Guards. The most complete guide to learning TypeScript ever built.
🌐
Tektutorialshub
tektutorialshub.com › home › typescript › typescript instanceof type guard
Typescript Instanceof Type Guard - Tektutorialshub
March 15, 2023 - Typescript instanceof operator checks if a value is an instance of a class/constructor function. It acts as TypeGuard & infers type in scope
Find elsewhere
🌐
EDUCBA
educba.com › home › software development › software development tutorials › typescript tutorial › typescript instanceof
TypeScript instanceof | Learn How instanceof works in TypeScript?
April 6, 2023 - The instanceof operator is used to validate the objects at runtime whenever we want to create the objects at the specific classes; also, it always returns the boolean values like true or false statements if the required class need to create ...
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Reddit
reddit.com › r/typescript › if typescript is duck-typed, how come when you use "instanceof", typescript accurately separate out the individual classes that implement the same interface? and how can we take advantage of this functionality in our typing?
r/typescript on Reddit: If typescript is duck-typed, how come when you use "instanceof", typescript accurately separate out the individual classes that implement the same interface? And how can we take advantage of this functionality in our typing?
October 23, 2024 -

Edit: I know that instanceof checks at runtime, I'm very familiar with it and have been using it for many years. My question is specifically how the typing system works in typescript, how/why typescript is able to discern via type inference the difference between Error and PseudoError. And, given that it can, whether it is possible to leverage that kind of discernment to create a utility function that's based on this rather than based on shapes and interfaces.

In lib.es5.d.ts in typescript we have:

interface Error {
    name: string;
    message: string;
    stack?: string;
}

Now in my typescript file, if I create a new type, like this:

class PseudoError {
    name = "hello"
    message = "world"
    stack? = "friends"
}

And I do the following:

const test : PseudoError | Error = new PseudoError()

type withoutError = Exclude<typeof test, Error> // <--- results to never, because of duck typing ?

The above is expected, because PseudoError and Error implement the same interface. So if I'm excluding Error, then I'm also excluding PseudoError by duck type standards.

But when I do this:

if (test instanceof Error) {
  test // <--- hover over = Error
} else {
  test // <--- hover over = PseudoError
}

It suggests that there is obviously some built-in functionality in typescript that doesn't work like in a duck-type-y way. This clearly aligns with the desired outcome in JS, but in my situation, I would like to have an Exclude<...> utility type that does the same thing as what typescript is doing behind the scenes with instanceof.

For example, where's this NotInstanceOf utility function?

const test : PseudoError | Error = new PseudoError()

type withoutError = NotInstanceOf<typeof test, Error> // <--- results to PsuedoError
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › classes
Class checking: "instanceof"
September 26, 2025 - The instanceof operator allows to check whether an object belongs to a certain class.
🌐
HowToDoInJava
howtodoinjava.com › home › typescript › typescript instanceof with class, array and interface
TypeScript instanceof with Class, Array and Interface
April 15, 2024 - In TypeScript, the instanceof operator checking the type of an object at runtime. It determines whether an object is an instance of a particular class or constructor function.
🌐
o7planning
o7planning.org › 14073 › typescript-instanceof
TypeScript instanceof operator | o7planning.org
The instanceof operator is used to check if an object is an object of a specified class.
🌐
Medium
medium.com › @seanbridger › advanced-typescript-7-10-guarding-types-with-typeof-instanceof-literals-c46c2668df45
Advanced TypeScript (7/10): Guarding Types with typeof/instanceof/literals | by Likely Coder | Medium
November 24, 2023 - function logMessage(message: string ... console.log(message.toFixed(2)); } } The instanceof operator checks if an object is an instance of a particular class or constructor function....
🌐
GeeksforGeeks
geeksforgeeks.org › typescript › typescript-instanceof-narrowing-type
TypeScript instanceof narrowing Type - GeeksforGeeks
April 28, 2025 - TypeScript instanceof operator is used for type narrowing, allowing us to check whether an object is an instance of a specific class or constructor function.
🌐
CoreUI
coreui.io › blog › what-is-the-difference-between-typeof-and-instanceof-in-javascript
What is the difference between typeof and instanceof in JavaScript · CoreUI
September 17, 2024 - The instanceof operator checks if an object is an instance of a specific class or constructor function.
🌐
2ality
2ality.com › 2017 › 08 › type-right.html
Beyond `typeof` and `instanceof`: simplifying dynamic type checks
August 18, 2017 - class PrimitiveNumber { static [Symbol.hasInstance](x) { return typeof x === 'number'; } } console.log(123 instanceof PrimitiveNumber); // true
🌐
Reddit
reddit.com › r/typescript › can instanceof operator be used to check if an object is an instance of a n interface, or type?
r/typescript on Reddit: Can instanceOf operator be used to check if an object is an instance of a n interface, or type?
August 10, 2023 -

I'm learning through [roadmap](https://roadmap.sh/typescript) and got stuck here (screenshot) where it says instanceOf can be used to check if an object is an instance of interface or type. I tried to do in code but it gives error.

The given code is invalid in TypeScript:

interface Person {
    name: string;
    age: number
}

const person = {
    name: "Ken",
    age: 25
}

if (person instanceof Person) // Error: 'Person' only refers to a type, but is being used as a value here.

So am I using wrong? or that is a typo in that website? Any help would be highly appreciated. Thank!

Top answer
1 of 3
5
To add to what others have said already, there's a neat trick you can do in typescript to check if your instance is of a given "type". You declare it as an abstract class and implement the Symbol.hasInstance method. Example: abstract class Person { abstract readonly name: string; abstract readonly age: number; public static [Symbol.hasInstance](instance: unknown): instance is Person { if (instance === null || (typeof instance !== 'object' && !(instance instanceof Person))) return false; const name: unknown = (instance as any).name; const age: unknown = (instance as any).age; if (typeof name !== 'string' && !(name instanceof String)) return false; return typeof age === 'number' || age instanceof Number; } } Then you can use it as: const person = { name: "SomeName", age: 55 }; const noPerson = { weight: "300kg" }; console.log(person instanceof Person); // will print true console.log(noPerson instanceof Person); // will print false The advantage of this method in my opinion is that because typescript has a structure typing system, you can just do: class PersonImpl implements Person { constructor( readonly name: string, readonly age: number ) { } } console.log(new PersonImpl("SomeName", 55) instanceof Person); // will also print true This way, if you make a typo on PersonImpl you will know immediately that it's wrong and at runtime you can still type check.
2 of 3
5
You might be interested in type predicates . https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates
🌐
GitHub
github.com › microsoft › TypeScript › issues › 58181
Proposal: Type-side use of `instanceof` keyword as an instance-query to allow instanceof checking · Issue #58181 · microsoft/TypeScript
April 13, 2024 - This feature would agree with the ...ript-Design-Goals · A new type-side keyword instanceof is proposed to allow an interface to be declared as mapping to a specific class or specific constructor variable....
Author   craigphicks