In this tutorial, I will explain how to check the type of an object in TypeScript using several methods. It is always good to know the type of an object before using it.
TypeScript’s type system is powerful, but it only helps at compile time. At runtime, JavaScript takes over, and type information disappears. This means that if you’re working with data from a web API, a user form, or even a local storage cache, you can’t always trust the shape or type of your objects. A simple mistake can lead to runtime errors that are hard to debug.
That’s why checking the type of an object at runtime is important in TypeScript.
Below are different methods to check the type of a TypeScript object.
Method 1: Using typeof for Primitive Types
The simplest way to check the type of a value in TypeScript (and JavaScript) is using the typeof operator. However, this only works for primitive types, like string, number, boolean, undefined, and symbol.
Example:
const zipCode = "90210";
if (typeof zipCode === "string") {
console.log("This is a string ZIP code.");
}
Here is the exact output, you can see in the screenshot below:

But:typeof does not work for objects, arrays, or custom types. If you try typeof on an object, you’ll just get "object" every time.
Check out How to Iterate Over an Object in TypeScript
Method 2: Using instanceof for Class Instances
If you’re working with objects created from classes, the instanceof operator is your friend. It checks whether an object is an instance of a specific class (including subclasses).
Example:
class Address {
constructor(public street: string, public city: string, public state: string) {}
}
const myAddress = new Address("1600 Pennsylvania Ave NW", "Washington", "DC");
if (myAddress instanceof Address) {
console.log("This is a valid Address object.");
}
You can see the exact output in the screenshot below:

When to use:
- When you have class-based models (for example, representing a U.S. address, a user profile, or an order).
- When you want to check inheritance.
Limitations:
- Does not work with plain objects or interfaces.
- Fails if objects are created in different JavaScript contexts (like iframes).
Check out How to Iterate Over a JSON Object in TypeScript
Method 3: Type Guard Functions for Interfaces and Object Shapes
Most real-world TypeScript code uses interfaces, not classes. Interfaces disappear at runtime, so you can’t use instanceof. Instead, I use type guard functions—custom functions that check if an object matches a specific shape.
Example: Checking a U.S. Address Object
Suppose you have an interface:
interface USAddress {
street: string;
city: string;
state: string;
zip: string;
}
You can write a type guard like this:
function isUSAddress(obj: any): obj is USAddress {
return (
typeof obj === "object" &&
obj !== null &&
typeof obj.street === "string" &&
typeof obj.city === "string" &&
typeof obj.state === "string" &&
typeof obj.zip === "string"
);
}
// Usage
const input = {
street: "350 Fifth Ave",
city: "New York",
state: "NY",
zip: "10118"
};
if (isUSAddress(input)) {
console.log(`Address is in ${input.state}.`);
} else {
console.log("Input is not a valid US address.");
}
Why this works:
Type guard functions give you full control over what you consider a valid object. You can check for required properties, value types, or even use regex for things like ZIP codes.
Read How to Convert TypeScript Objects to JSON
Method 4: Discriminated Unions (Tagged Unions)
If you have several object types that share some properties but differ in others, discriminated unions are a great way to check types in TypeScript.
Example:
type User = {
type: "user";
name: string;
email: string;
};
type Admin = {
type: "admin";
name: string;
email: string;
adminLevel: number;
};
type Person = User | Admin;
function handlePerson(person: Person) {
if (person.type === "admin") {
// TypeScript knows this is an Admin
console.log(`Admin level: ${person.adminLevel}`);
} else {
// TypeScript knows this is a User
console.log(`User email: ${person.email}`);
}
}
When to use:
- When you want TypeScript to narrow down the type automatically.
- When your objects have a clear, unique property (like
type) that distinguishes them.
Check out How to Push Objects into an Array in TypeScript?
Method 5: Using Object.prototype.toString.call
For some built-in objects, like arrays or dates, typeof is not enough. Here’s a trick I often use:
const arr = [1, 2, 3];
const date = new Date();
console.log(Object.prototype.toString.call(arr)); // [object Array]
console.log(Object.prototype.toString.call(date)); // [object Date]
You can wrap this in a helper function:
function isArray(obj: any): obj is any[] {
return Object.prototype.toString.call(obj) === "[object Array]";
}
Note:
For most array checks, use Array.isArray() instead.
Read How to Sort Arrays of Objects in TypeScript
Method 6: Using Third-Party Libraries (e.g., io-ts, zod)
For large projects or when you need runtime type validation (for example, validating API responses), I sometimes use libraries like zod or io-ts.
Example with Zod:
import { z } from "zod";
const USAddressSchema = z.object({
street: z.string(),
city: z.string(),
state: z.string(),
zip: z.string().regex(/^\d{5}(-\d{4})?$/), // US ZIP code regex
});
const result = USAddressSchema.safeParse({
street: "1 Infinite Loop",
city: "Cupertino",
state: "CA",
zip: "95014"
});
if (result.success) {
console.log("Valid US address:", result.data);
} else {
console.error("Invalid address:", result.error);
}
Why use a library?
- Automatic error messages
- Complex nested validation
- Reusable schemas
Common Best Practices
- Don’t rely on
typeoffor objects or arrays. It just returns"object"for anything that’s not a primitive. - Always validate external data. If you’re getting data from an API, never trust its shape.
- Prefer interfaces and type guards for flexibility. They make your code more maintainable and readable.
- Use discriminated unions for related object types. This makes your code safer and more expressive.
- Consider using libraries for complex validation. Especially if your app handles lots of user input or third-party data.
Wrapping Up
As a TypeScript developer, you should know how to check the type of an object in TypeScript using various methods. I hope the above examples help you. If you have any questions or want to share your own tips for checking object types in TypeScript, feel free to drop a comment below.
You may like the following tutorials:
- Add Property to Object in TypeScript
- How to Merge Two Objects in TypeScript?
- How to Merge Object Arrays Without Duplicates in TypeScript?

Hey! I’m Bijay Kumar, founder of SPGuides.com and a Microsoft Business Applications MVP (Power Automate, Power Apps). I launched this site in 2020 because I truly enjoy working with SharePoint, Power Platform, and SharePoint Framework (SPFx), and wanted to share that passion through step-by-step tutorials, guides, and training videos. My mission is to help you learn these technologies so you can utilize SharePoint, enhance productivity, and potentially build business solutions along the way.