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
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
Discussions

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
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › typescript › typescript-instanceof-operator
TypeScript Instanceof Operator - GeeksforGeeks
July 22, 2024 - The TypeScript instanceof operator checks if an object is an instance of a specified class or constructor function at runtime.
Find elsewhere
🌐
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.
🌐
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
🌐
Ultimate Courses
ultimatecourses.com › blog › understanding-typescript-instanceof-type-guard
Understanding TypeScript: instanceof Type Guard - Ultimate Courses
August 10, 2019 - 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.
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › narrowing.html
TypeScript: Documentation - Narrowing
While we won’t dive deep here, and you’ll see more of this when we get into classes, they can still be useful for most values that can be constructed with new. As you might have guessed, instanceof is also a type guard, and TypeScript narrows in branches guarded by instanceofs.
🌐
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.
🌐
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.
🌐
Tim Mousk
timmousk.com › blog › typescript-instanceof-interface
How To Use InstanceOf On An Interface In TypeScript? – Tim Mouskhelichvili
March 6, 2023 - Unfortunately, you cannot use the instanceOf keyword on an interface in TypeScript.
🌐
Havrlant
log.havrlant.cz › typescript-switch-instanceof
How to use instanceof in switch statement in TypeScript
October 17, 2020 - The problem with the shape instanceof Point is that it cannot be simply transformed into the switch statement.
🌐
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.
🌐
W3Schools
w3schools.com › jsref › jsref_oper_instanceof.asp
W3Schools.com
const cars = ["Saab", "Volvo", "BMW"]; (cars instanceof Array) // Returns true (cars instanceof Object) // Returns true (cars instanceof String) // Returns false (cars instanceof Number) // Returns false Try it Yourself »
🌐
Medium
medium.com › @samjwright › why-is-instanceof-my-object-returning-false-in-typescript-fec74df5c2a8
Why is instanceof my object returning false in TypeScript? | by Sam Wright | Medium
September 17, 2021 - Let’s imagine you throw an error within JSDom and assign it to the variable err. Outside of JSDom, we want to check whether err is an instance of Error, which we do like this err instanceof Error.
🌐
Cloudaffle
cloudaffle.com › blog › instanceof-type-guard-typescript
Understanding `instanceof` Type Guard in TypeScript
April 22, 2023 - When you use instanceof in a conditional expression, TypeScript automatically narrows the type of the object within the block, allowing you to access and use properties and methods specific to that class without causing type errors.
🌐
Ebeced
ebeced.com › blog › ts-how › instance-type-of
TypeScript: How to use instanceof, typeof and in
March 5, 2023 - The instanceof operator is used to determine whether an object is an instance of a particular class or interface. It is often used in TypeScript to check whether an object conforms to a certain interface or has a particular class as its prototype.
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › classes.html
TypeScript: Documentation - Classes
instanceof will be broken between instances of the subclass and their instances, so (new MsgError()) instanceof MsgError will return false.