Create an Object from an Interface in TypeScript

You define an interface for your API response or form data, but then you pause. How exactly do you create an object that follows it? This is a common situation when you start using TypeScript for type safety.

An interface in TypeScript acts like a blueprint. It tells your code what shape an object should have. But it does not create the object itself; you still need to do that manually in the right way.

In this article, I’ll show you 4 ways to create an object from an interface in TypeScript.

Method 1 – Direct Object Assignment

This is the simplest and most common approach. You use it when you already know all the values at the time of object creation.

Step 1: Define an interface

interface User {
id: number;
name: string;
isActive: boolean;
}

How does this code work?
The User interface defines a structure. Any object of this type must have id, name, and isActive with the correct types.

Step 2: Create an object using the interface

const user: User = {
id: 1,
name: "John",
isActive: true
};
console.log(user);

How does this code work?
The TypeScript variable user is explicitly typed as User. TypeScript checks that the object matches the interface exactly.

Output:

{ id: 1, name: "John", isActive: true }

I executed the above example code and added the screenshot below.

Create an Object from an Interface TypeScript

Pro Tip: Enable strict mode in your TypeScript config to catch missing or incorrect properties early.

Method 2 – Use a Function to Create Objects

Use this method when you need reusable logic, like creating objects from form input or API data.

Step 1: Define the interface

interface User {
id: number;
name: string;
isActive: boolean;
}

Step 2: Create a factory function

function createUser(id: number, name: string): User {
return {
id,
name,
isActive: true
};
}

Step 3: Call the function

const user = createUser(2, "Rahul");
console.log(user);

How does this code work?
The function createUser returns an object of type User. TypeScript ensures the returned object matches the interface.

Output:

{ id: 2, name: "Rahul", isActive: true }

I executed the above example code and added the screenshot below.

Create an Object from an Interface in TypeScript

Pro Tip: Use factory functions when working with APIs or forms. It keeps your TypeScript object creation consistent and clean.

Method 3 – Use a Class that Implements the Interface

Choose this when you need behavior (methods) along with data, not just a plain object.

Step 1: Define the interface

interface User {
id: number;
name: string;
isActive: boolean;
}

Step 2: Create a class using implements

class UserModel implements User {
id: number;
name: string;
isActive: boolean;

constructor(id: number, name: string) {
this.id = id;
this.name = name;
this.isActive = true;
}
}

Step 3: Create an instance

const user = new UserModel(3, "Anita");

How does this code work?
The implements keyword forces the class to follow the User interface. The constructor initializes the object.

Output:

UserModel { id: 3, name: "Anita", isActive: true }

I executed the above example code and added the screenshot below.

Create an TypeScript Object from an Interface

Pro Tip: Use TypeScript classes when you need methods like activateUser() or deactivateUser() along with properties.

This method is useful when working with dynamic data, but you must be careful.

Step 1: Define the interface

interface User {
id: number;
name: string;
isActive: boolean;
}

Step 2: Create object using assertion

const user = {} as User;

user.id = 4;
user.name = "Sneha";
user.isActive = false;

How does this code work?
The as User tells TypeScript to treat the object as a User, even if it is initially empty.

Output:

{ id: 4, name: "Sneha", isActive: false }

Pro Tip: Avoid this method unless necessary. It can bypass type safety and lead to runtime bugs.

Things to Keep in Mind

  • Missing properties cause errors: TypeScript will fail compilation if required fields are not provided.
  • Optional properties need ?: Use name?: string when a field is not always present.
  • Avoid any type: Using any removes all type safety and defeats the purpose of interfaces.
  • Type assertion is risky: It skips checks and can introduce hidden bugs.
  • Strict null checks matter: With strict mode on, null and undefined must be handled explicitly.
  • Interfaces vs type aliases: Interfaces work best for object shapes, while type aliases handle unions and advanced patterns.

You learned four practical ways to create an object from an interface in TypeScript, from simple assignment to class-based models. Use direct assignment for quick objects, factory functions for reusable logic, and classes when you need behavior. I hope you found this article helpful.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.