Today, we will discuss a very common requirement that TypeScript developers get. In this tutorial, I will explain how to check if an object implements an interface in TypeScript. There are various methods to do so.
Understanding TypeScript Interfaces
Let me first explain to you what is an interface in TypeScript. Interfaces are a feature of TypeScript that allows us to define the structure or shape of an object and specify the properties and methods that an object has. They provide a way to enforce type checking and ensure that objects conform to a specific contract.
Here’s a simple example of an interface:
interface Person {
name: string;
age: number;
address: string;
}
Read Remove a Property from an Object in TypeScript
Check if an Object Implements an Interface in TypeScript
Now, let me explain to you how to check if an object implements an interface in TypeScript using different methods.
Method 1: Type Guard Function
One way to check if an object implements an interface is by creating a type guard function in TypeScript. A type guard function manually checks if the object conforms to the expected structure of the interface. It takes an object as input and returns a boolean indicating whether the object satisfies the interface.
Here’s an example:
interface USAAddress {
street: string;
city: string;
state: string;
zipCode: string;
}
function isUSAAddress(obj: any): obj is USAAddress {
return (
obj &&
typeof obj.street === 'string' &&
typeof obj.city === 'string' &&
typeof obj.state === 'string' &&
typeof obj.zipCode === 'string'
);
}
const johnAddress = {
street: '123 Main St',
city: 'New York',
state: 'NY',
zipCode: '10001',
};
if (isUSAAddress(johnAddress)) {
console.log('The object implements the USAAddress interface.');
} else {
console.log('The object does not implement the USAAddress interface.');
}
In this example, we define an interface called USAAddress
representing an address in the United States. We then create a type guard function isUSAAddress
that checks if an object conforms to the USAAddress
interface. If all the properties match the expected types, the function returns true
; otherwise, it returns false
.
I executed the above TypeScript code using VS code, and you can see the exact output in the screenshot below:

Check out Optional Parameters in TypeScript Interfaces
Method 2: Inline Type Assertion
Another approach is to use inline type assertion to check if an object implements an interface in TypeScript. This method involves manually checking the properties of the object against the interface.
Here’s an example:
interface Employee {
name: string;
employeeId: number;
department: string;
}
const sarah = {
name: 'Sarah Johnson',
employeeId: 12345,
department: 'Marketing',
};
if (
(sarah as Employee).name &&
(sarah as Employee).employeeId &&
(sarah as Employee).department
) {
console.log('The object implements the Employee interface.');
} else {
console.log('The object does not implement the Employee interface.');
}
In this example, we define an Employee
interface representing an employee in a company. We then use inline type assertion (sarah as Employee)
to check if the sarah
object has all the required properties defined in the Employee
interface. If all the properties are present, the condition evaluates to true
, indicating that the object implements the interface.
Here is the exact output in the screenshot below:

It’s important to note that TypeScript interfaces are not available at runtime. They are a compile-time construct used for type checking and are removed during the compilation process. Therefore, using the instanceof
operator to check if an object is an instance of an interface will not work.
Conclusion
In this tutorial, I explained two methods to check if an object implements an interface in TypeScript. The first method involves creating a type guard function that manually checks the object’s properties against the interface. The second method uses inline type assertion to perform the same check. Do let me know in the comment below if it helps.
You may also like:
- Check if an Object is Type of Interface in TypeScript
- TypeScript Interface Function Properties
- Create an Object from an Interface in TypeScript
I’m Bijay Kumar Sahoo, and I am honored to be awarded the Microsoft MVP. With over 18 years of experience in the IT industry, I got a chance to work on SharePoint Framework (SPFx) development, TypeScript, React, JavaScript, etc. My journey has taken me through esteemed organizations such as TCS, HP, and KPIT, where I have honed my skills and expanded my expertise. Check out more about me here.