4.19.4 The instanceof operator

The instanceof operator requires the left operand to be of type Any, an object type, or a type parameter type, and the right operand to be of type Any or a subtype of the 'Function' interface type. The result is always of the Boolean primitive type.

So you could use

mySprite instanceof Sprite;

Note that this operator is also in ActionScript but it shouldn't be used there anymore:

The is operator, which is new for ActionScript 3.0, allows you to test whether a variable or expression is a member of a given data type. In previous versions of ActionScript, the instanceof operator provided this functionality, but in ActionScript 3.0 the instanceof operator should not be used to test for data type membership. The is operator should be used instead of the instanceof operator for manual type checking, because the expression x instanceof y merely checks the prototype chain of x for the existence of y (and in ActionScript 3.0, the prototype chain does not provide a complete picture of the inheritance hierarchy).

TypeScript's instanceof shares the same problems. As it is a language which is still in its development I recommend you to state a proposal of such facility.

See also:

  • MDN: instanceof
  • TypeScript's docs: instanceof Narrowing, which shows how instanceof can be used to narrow things
Answer from Zeta on Stack Overflow
🌐
Upmostly
upmostly.com › home › angular › type checking in typescript
Type Checking In Typescript - Upmostly
November 1, 2022 - Typescript has a way to deal with this however. It’s called “Type Guards”, and it allows you to write code that will not only check an object is a given type, but that Typescript from that point on can treat the variable as the type.
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › everyday-types.html
TypeScript: Documentation - Everyday Types
Reminder: Because type assertions are removed at compile-time, there is no runtime checking associated with a type assertion. There won’t be an exception or null generated if the type assertion is wrong. TypeScript only allows type assertions which convert to a more specific or less specific version of a type.
Discussions

How do I test for type?
A different angle from the other answer. Instead of asking "how do I test for the type", I'm going to ask "what do you want to do differently?" If doSomething() just cares about the color, then you don't actually have to test anything. If your types need to actually behave differently, they should be "truly distinct" in the code, not just in typescript. The compiled javascript has no understanding of a "Jacket" or a "Scalf" in your example Your options are multi-faceted. You could have a type field as the other poster said, testing on that. They could be class objects instead of naked JSON. That way the object is aware of how to doSomething() You could move the distinguishing business logic out of doSomething and into the objects. This seems worse than #2 in most cases, but can be better if you actually expect to have a large number of distinct object behaviors with fewer consistencies between them. Good Typescript code has to be Good Javascript code that happens to have types at compile time. More on reddit.com
🌐 r/typescript
22
12
January 24, 2024
How to get the type of an object?
You can do this as long as you check instanceof against a class. You can't do that against an interface since it's an interface and not a real value type interface IMyInterface { foo: string; } class MyClass { constructor(public bar: string){} } function random(request: IMyInterface | MyClass): string{ if (request instanceof MyClass) { return request.bar; } else { return request.foo; } } playground link if you really need to get the instanceof for the other one, you need to have that interface implemented on a class, and then use that as a possible type in the function interface IMyInterface { foo: string; } class MyClassImplimented implements IMyInterface { constructor(public foo: string){} } class MyClass { constructor(public bar: string){} } function random(request: MyClassImplimented | MyClass): string{ if (request instanceof MyClassImplimented) { return request.foo; } else { return request.bar; } } playground link More on reddit.com
🌐 r/typescript
7
3
June 1, 2023
Top answer
1 of 5
640

4.19.4 The instanceof operator

The instanceof operator requires the left operand to be of type Any, an object type, or a type parameter type, and the right operand to be of type Any or a subtype of the 'Function' interface type. The result is always of the Boolean primitive type.

So you could use

mySprite instanceof Sprite;

Note that this operator is also in ActionScript but it shouldn't be used there anymore:

The is operator, which is new for ActionScript 3.0, allows you to test whether a variable or expression is a member of a given data type. In previous versions of ActionScript, the instanceof operator provided this functionality, but in ActionScript 3.0 the instanceof operator should not be used to test for data type membership. The is operator should be used instead of the instanceof operator for manual type checking, because the expression x instanceof y merely checks the prototype chain of x for the existence of y (and in ActionScript 3.0, the prototype chain does not provide a complete picture of the inheritance hierarchy).

TypeScript's instanceof shares the same problems. As it is a language which is still in its development I recommend you to state a proposal of such facility.

See also:

  • MDN: instanceof
  • TypeScript's docs: instanceof Narrowing, which shows how instanceof can be used to narrow things
2 of 5
159

TypeScript have a way of validating the type of a variable in runtime. You can add a validating function that returns a type predicate. So you can call this function inside an if statement, and be sure that all the code inside that block is safe to use as the type you think it is.

Example from the TypeScript docs:

function isFish(pet: Fish | Bird): pet is Fish {
   return (<Fish>pet).swim !== undefined;
}

// Both calls to 'swim' and 'fly' are now okay.
if (isFish(pet)) {
  pet.swim();
}
else {
  pet.fly();
}

See more at: https://www.typescriptlang.org/docs/handbook/advanced-types.html

🌐
JavaScript in Plain English
javascript.plainenglish.io › how-to-check-if-an-object-is-a-certain-type-in-typescript-97e6a72c8e57
How to check if an object is a certain type in TypeScript? | by Sean Amarasinghe | JavaScript in Plain English
November 21, 2024 - How to check if an object is a certain type in TypeScript? In TypeScript, you can check if an object is of a certain type using type predicates and type guards. Here’s how you can do it: 1. Using …
🌐
2ality
2ality.com › 2025 › 02 › testing-types-typescript.html
Testing types in TypeScript
February 24, 2025 - Run the tests via a tool such as tsx that type checks code before running it. Overview of various type testing approaches: 2ality blog post “Testing static types in TypeScript”
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › typeof-types.html
TypeScript: Documentation - Typeof Type Operator
TypeScript adds a typeof operator you can use in a type context to refer to the type of a variable or property:
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › typescript › how-to-check-the-type-of-an-object-in-typescript
How to Check the Type of an Object in Typescript ? - GeeksforGeeks
July 23, 2025 - By using the as keyword in the return type of a function, you can create a type predicate that helps TypeScript infer the type of an object. Example: The below example demonstrates how to create and use a user-defined type predicate to check ...
🌐
Medium
medium.com › @devnotesbyharini › understanding-typescript-type-checking-in-functions-a-beginners-insight-0f920c2a5818
Understanding TypeScript Type Checking in Functions: A Beginner’s Insight | by Harini Murali | Medium
July 24, 2025 - TypeScript’s type checking ensures clean, reliable code by enforcing consistent types in functions. Without annotations, parameters default to any, offering no safety. Adding parameter types and using inferred or explicit return types improves ...
🌐
Zero To Mastery
zerotomastery.io › blog › typescript-type-checking
Type Checking In TypeScript: A Beginners Guide | Zero To Mastery
January 26, 2024 - Since both classes have a name property, TypeScript would correctly identify that they have the same shape, if the properties were public. But by making the properties private, it hides them, so TypeScript isn't able to compare the shape of FirstName and LastName. This is exactly what we need because we want the type checking to fail if we swap the first name and last name accidentally.
🌐
GeeksforGeeks
geeksforgeeks.org › typescript › how-to-check-types-in-typescript
How to Check Types in Typescript? - GeeksforGeeks
July 23, 2025 - Checking types in TypeScript involves methods like typeof for primitive types, instanceof for class instances, and custom type guards for complex type validation.
🌐
GitHub
github.com › janjakubnanista › ts-type-checked
GitHub - janjakubnanista/ts-type-checked: Runtime duck type checking utilities for TypeScript. · GitHub
ts-type-checked generates type guards based on your own (or library) TypeScript types.
Starred by 89 users
Forked by 5 users
Languages   TypeScript 92.9% | Shell 3.3% | JavaScript 3.2% | Makefile 0.6%
🌐
DEV Community
dev.to › onureren › how-to-check-against-custom-types-in-typescript-hh2
How To Check Against Custom Types In TypeScript - DEV Community
July 3, 2023 - TypeScript only exists is the compile-time and it is completely discarded in the run-time. This means you CANNOT use typeof keyword to check for custom types.
🌐
Reddit
reddit.com › r/typescript › how do i test for type?
r/typescript on Reddit: How do I test for type?
January 24, 2024 -

How do I test for the type passed to the doSomething() function? Is this where I would used a type predicate?

type Jacket = {
    size: number,
    colour: string
}

type Scalf = {
    length: number,
    colour: string
}

function doSomething(input: Jacket | Scalf) {
    // How do I test for the type of input?
}
Top answer
1 of 12
21
type Jacket = { size: number, colour: string } type Scalf = { length: number, colour: string } function doJacket(jacket: Jacket) {} function doScalf(scalf: Scalf) {} function doSomething(input: Jacket | Scalf) { // type narrowing if ("size" in input) doJacket(input); if ("length" in input) doScalf(input); } it appears a bit arbitrary to pick size or length for type narrowing, so usually you go with a common property that is shared between both and has a const type like string literal, known as discriminator. If used as union this Jacket | Scalf is then called a discriminated union. type Jacket = { type: 'jacket', //discriminator size: number, colour: string } type Scalf = { type: 'scalf', //discriminator length: number, colour: string } type Stuff = Jacket | Scalf; //discriminated union function doJacket(jacket: Jacket) {} function doScalf(scalf: Scalf) {} function doSomething(input: Stuff) { if (input.type === 'jacket') doJacket(input); if (input.type === 'scalf') doScalf(input); } but that's not always possible, so you either keep using if ("size" in input) or you write your own functions (called type guards) that do it // type guard function isJacket(input: Stuff): input is Jacket { return size" in input; } function doSomething(input: Stuff) { if (isJacket(input)) doJacket(input); } this allows to have the check at a central place and still make TypeScript aware of the type narrowing.
2 of 12
13
A different angle from the other answer. Instead of asking "how do I test for the type", I'm going to ask "what do you want to do differently?" If doSomething() just cares about the color, then you don't actually have to test anything. If your types need to actually behave differently, they should be "truly distinct" in the code, not just in typescript. The compiled javascript has no understanding of a "Jacket" or a "Scalf" in your example Your options are multi-faceted. You could have a type field as the other poster said, testing on that. They could be class objects instead of naked JSON. That way the object is aware of how to doSomething() You could move the distinguishing business logic out of doSomething and into the objects. This seems worse than #2 in most cases, but can be better if you actually expect to have a large number of distinct object behaviors with fewer consistencies between them. Good Typescript code has to be Good Javascript code that happens to have types at compile time.
🌐
React
legacy.reactjs.org › docs › static-type-checking.html
Static Type Checking – React
For this reason, we recommend using Flow or TypeScript instead of PropTypes for larger code bases. Flow is a static type checker for your JavaScript code. It is developed at Facebook and is often used with React. It lets you annotate the variables, functions, and React components with a special type syntax, and catch mistakes early.
🌐
Turing
turing.com › kb › check-type-of-objects-variables-in-typescript
How to Check Type of Objects & Variables in Typescript
Using operators such as the in operator, instanceof operator, or typeof operator can make it even easier for other developers to understand and modify your code · In TypeScript, you can use several operators to check the type of objects and variables.
🌐
DanyWalls
danywalls.com › how-to-check-types-in-typescript
How To Check Types In Typescript | DanyWalls
August 10, 2022 - Click here to deploy your Angular apps for free on a reliable VPS · In Typescript, we have three ways to work with it using: typeof: the keyword helps to check value types, like boolean, string, number, etc.
🌐
DEV Community
dev.to › danywalls › how-to-check-types-in-typescript-o5n
How To Check Types In Typescript - DEV Community
August 31, 2022 - In Typescript, we have to check ... Typescript, we have three ways to work with it using: typeof: the keyword helps to check values types, like boolean, string, number, etc....