Short answer

(Almost) all type information are removed after compilation, and you can't use instanceof operator with an operand (T in your example) that does not exist at runtime.

Long answer

An identifier in TypeScript could belong to one or more of the following groups: type, value, namespace. What gets emitted as JavaScript is identifiers in the group of value.

Thus runtime operators only works with values. So if you want to do runtime type checking of the value of foo, you'll need to do the hard work yourself.

See the Declaration Merging section of the TypeScript Handbook for more information.

Answer from vilicvane on Stack Overflow
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
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
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
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
🌐
Learn TypeScript
learntypescript.dev › 07 › l4-instanceof-type-guard
Using an `instanceof` type guard | Learn TypeScript
... The expression returns true or false depending on whether the object belongs to the class or not. The TypeScript compiler uses an instanceof expression to narrow the variable's type in the expression.
🌐
Tektutorialshub
tektutorialshub.com › home › typescript › typescript instanceof type guard
Typescript Instanceof Type Guard - Tektutorialshub
March 15, 2023 - instanceof is an operator in TypeScript, which checks if an object is an instance of a specific class or a constructor function. The Instanceof also takes inheritance into account.
🌐
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 ...
🌐
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
🌐
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.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › 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.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › typescript tutorial › typescript instanceof
TypeScript instanceof | Learn How instanceof works in TypeScript?
April 6, 2023 - Basically, the instanceof is the advanced type of the typescript; meanwhile, the operator is tested to see if the prototype property for the class-based constructors appears anywhere in the objects’ prototype chain.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › instanceof
instanceof - JavaScript - MDN Web Docs
July 8, 2025 - } Rectangle.prototype = Object.create(Shape.prototype); Rectangle.prototype.constructor = Rectangle; const rect = new Rectangle(); rect instanceof Object; // true rect instanceof Shape; // true rect instanceof Rectangle; // true rect instanceof String; // false const literalObject = {}; const nullObject = Object.create(null); nullObject.name = "My object"; literalObject instanceof Object; // true, every object literal has Object.prototype as prototype ({}) instanceof Object; // true, same case as above nullObject instanceof Object; // false, prototype is end of prototype chain (null) The following code creates an object type Car and an instance of that object type, myCar.
🌐
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.
🌐
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
🌐
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....
🌐
Convex
convex.dev › typescript 101 › control flow & operators › instanceof operator
Instanceof Operator | TypeScript Guide by Convex
Unlike typeof, which only reveals basic types like "object" or "string", instanceof traverses the entire prototype chain. This makes it perfect for working with inheritance hierarchies in object-oriented code. When working with TypeScript projects using Convex, you can use this pattern to implement polymorphic behavior while maintaining type safety—a common need in end-to-end typed applications.
🌐
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
🌐
Reddit
reddit.com › r/typescript › type .kind checks vs. class instanceof checks
r/typescript on Reddit: type .kind checks vs. class instanceof checks
May 8, 2022 -

Hello all,

Straight to the Point

I've recently started diving into TypeScript and at a high level I'm trying to achieve effective pattern matching. In short my question is: to achieve 'discriminated unions' (much like enums in Rust) should types with a kind property be used, or simply data classes that can be discriminated with instanceof.

.kind Property

I've encountered older docs that illustrate defining types with a kind property on which to discriminate.

type Shape = Square | Circle

type Square = {
  kind: "square",
  side: number 
}

type Circle = {
  kind: "circle", 
  radius: number 
}

function printShapeProp(shape: Shape) {
  if (shape.kind === 'square') {
    console.log(shape.side)
  } else if (shape.kind === 'circle') {
    console.log(shape.radius)
  }
}

Class with instanceof

However newer docs don't seem to ever show discriminated unions, and it's starting to seem like the idiomatic way is to just define simple data classes and leverage the builtin instanceof operator.

class Shape {}

class Square extends Shape {
  side: number

  constructor (side: number) {
    super()
    this.side = side
  }
}

class Circle extends Shape {
  radius: number

  constructor (radius: number) {
    super()
    this.radius = radius
  }
}

function printShapeProp(shape: Shape) {
  if (shape instanceof Circle) {
    console.log(shape.radius)
  } else if (shape instanceof Square) {
    console.log(shape.side)
  }
}

Observations

One thing I noticed was the VSCode was much better at providing completion for shape.kind === '<choices>' than shape instanceof <choices>, where the former just showed me the possible values of circle and square, and the latter showed me a whole bunch of extraneous options.

Further I understand instanceof comes with many pitfalls, but I'm not sure they are relevant given your types are constrained to classes. From here I can only imagine the difference is in how flexible structural typing (type aliases) is compared to nominal typing (class names).

🌐
Bobby Hadz
bobbyhadz.com › blog › typescript-instanceof-only-refers-to-type-but-is-being-used-as-value
(instanceof) 'X' Only refers to a type, but is being used as a value here | bobbyhadz
February 29, 2024 - If you need to test if a specific object correctly implements a type, use a user-defined type guard. ... Copied!interface Employee { id: number; name: string; salary: number; } function isAnEmployee(obj: any): obj is Employee { return 'id' in obj && 'name' in obj && 'salary' in obj; } const emp: Employee = { id: 3, name: 'Bobby Hadz', salary: 300, }; console.log(isAnEmployee(emp)); // 👉️ true console.log(isAnEmployee({ id: 1 })); // 👉️ false if (isAnEmployee(emp)) { // 👉️ TypeScript knows that emp is type Employee console.log(emp.id); // 👉️ 3 console.log(emp.name); // 👉️ "Bobby Hadz" console.log(emp.salary); // 👉️ 300 }
🌐
2ality
2ality.com › 2017 › 08 › type-right.html
Beyond `typeof` and `instanceof`: simplifying dynamic type checks
August 18, 2017 - > typeof {} 'object' > typeof function () {} 'function' This quirk, combined with the previous quirk means that there is no simple way to check for object-ness via typeof. ... Given the class PrimitiveNumber, the following code configures for which values x the expression x instanceof PrimitiveNumber returns true.
🌐
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. It also takes inheritance into account. Such a check may be necessary in many cases. For example, it can be used for building a polymorphic function, the one ...