VOOZH about

URL: https://blog.logrocket.com/when-how-use-interfaces-classes-typescript/

โ‡ฑ When and how to use interfaces and classes in TypeScript - LogRocket Blog


2021-11-25
1156
#typescript
Gapur Kassym
79003
๐Ÿ‘ Image

See how LogRocket's Galileo AI surfaces the most severe issues for you

No signup required

Check it out

Interfaces and classes are the fundamental parts of object-oriented programming (OOP). TypeScript is an object-oriented JavaScript language that, from ES6 and later, supports OOP features like interface, class, and encapsulation.

๐Ÿ‘ Image

But when should we use interfaces, classes, or both at the same time? If you are a new or confused using interfaces and classes, this piece is for you.

In this article, Iโ€™ll show you what interfaces and classes are and when to use one or both of them in TypeScript.

๐Ÿš€ Sign up for The Replay newsletter

The Replay is a weekly newsletter for dev and engineering leaders.

Delivered once a week, it's your curated guide to the most important conversations around frontend dev, emerging AI tools, and the state of modern software.

What is a class in TypeScript?

Before we get started, we need to know what a TypeScript class is. In object-oriented programming, a class is a blueprint or template by which we can create objects with specific properties and methods.

Typescript provides additional syntax for type checking and converts code to clean JavaScript that runs on any platform and browser. Classes are involved in all stages of code. After converting the TypeScript code to a JavaScript file, you can find them in the final files.

The class defines the template of the object, or what it is and what it does. Letโ€™s create a simple class with properties and methods so we can see how it will behave.

First, Iโ€™m going to create a Developer class through the following lines of code:

class Developer {
 name?: string; // string or undefined
 position?: string; // string or undefined
}

We describe the class with properties name and position. They contain types like string and undefined.

Next, letโ€™s create an object via the Developer class using the new keyword:

const developer = new Developer();
developer.name // it outputs undefined
developer.position // it outputs undefined

When we call developer.name, it returns undefined because we didnโ€™t assign initial values. In order to create an object with values in TypeScript, we can use the constructor method. The constructor method is used to initialize and create objects.

Now we update our Developer class with the following code:

class Developer {
 name: string; // only string
 position: string; // only string

 constructor(name: string, position: string) {
 this.name = name;
 this.position = position;
 }
}

In the code above, we added the constructor method to initialize the properties with values.

Now we are able to set the name as Gapur and position as Frontend Developer using the following code:

const developer = new Developer("Gapur", "Frontend Developer");
developer.name // it outputs Gapur
developer.position // it outputs Frontend Developer

Last, as I mentioned earlier, the class has methods that how the object should act. In this case, any developer develops applications, therefore, the Developer class has the method develop.

Thus, a developer object can perform a development action:

class Developer {
 name: string;
 position: string;

 constructor(name: string, position: string) {
 this.name = name;
 this.position = position;
 }

 develop(): void {
 console.log('develop an app');
 }
}

If we run the develop method, it will execute the following console.log statement:

developer.develop() // it outputs develop an app

What is an interface in TypeScript?

An interface is a structure that acts like a contract in your application, or the syntax for classes to follow. The interface is also known as duck printing, or subtyping.

The interface includes an only method and field declarations without implementation. We canโ€™t use it to create anything. A class that implements an interface must have all fields and methods. Therefore, we use them for type checking.


Over 200k developers use LogRocket to create better digital experiences

๐Ÿ‘ Image
Learn more โ†’

When TypeScript converts all code to JavaScript, the interface will disappear from the JavaScript file. Therefore, it is a helpful tool during the development phase.

We should use an interface for the following:

  • Validating specific structure of properties
  • Objects as parameters
  • Objects returned from functions

Now, letโ€™s declare the interface through the following lines of code:

interface InterfaceName {
 // variables;
 // methods;
}

We can only contain declarations of variables and methods in the body of the interface. Letโ€™s create an IDeveloper interface for the previous Developer class:

interface IDeveloper {
 name: string
 position: string
 develop: () => void
}

class Developer implements IDeveloper {
 name: string;
 position: string;

 constructor(name: string, position: string) {
 this.name = name;
 this.position = position;
 }

 develop(): void {
 console.log('develop an app');
 }
}

In the above code, our IDeveloper interface contains the variables name and position. It also includes the develop method. So the Developer class implements the IDeveloper interface. Thus, it must define two variables and a method.

If the Developer class doesnโ€™t implement any variables, TypeScript will show an error:

class Developer implements IDeveloper {
 // error Class 'Developer' incorrectly implements interface 'IDeveloper'.
 name: string;

 constructor(name: string, position: string) {
 this.name = name;
 this.position = position;
 }

 develop(): void {
 console.log('develop an app');
 }
}

Interfaces vs classes

So when should we use classes and when should we use interfaces?

Before we start, I want to share with you the powerful TypeScript static property that allow us to use fields and methods of classes without creating instance of class.

I am going to make a class with a static method using the previous Developer class:

class Developer {
 static develop(app: { name: string, type: string }) {
 return { name: app.name, type: app.type };
 }
}

Now, we can just call the Developer.develop() method without instantiating the class:

Developer.develop({ name: 'whatsapp', type: 'mobile' })
// outputs: { "name": "whatsapp", "type": "mobile" } 

Great!

Also, we can use classes for type checking in TypeScript. Letโ€™s create an App class using the following code:

class App {
 name: string;
 type: string;

 constructor(name: string, type: string) {
 this.name = name;
 this.type = type;
 }
}

Letโ€™s modify our Developer class:

class Developer {
 static develop(app: App) {
 return { name: app.name, type: app.type }; // output the same
 }
}

Now I will make an App instance and invoke Developer.develop() with an argument object:

const app = new App('whatsapp', 'mobile');
Developer.develop(app);
// outputs the same: { "name": "whatsapp", "type": "mobile" } 

Developer.develop(app) and Developer.develop({ name: 'whatsapp', type: 'mobile' }) output the same content. This is awesome, but the second approach is more readable and flexible.

Plus, we can check the type of arguments. Unfortunately, to do so, we need to create an object. So how can we improve it? This is where the interface comes in!

First, I am going to change the App class to an interface with the following code:

interface App {
 name: string
 type: string
}

class Developer {
 static develop(app: App) {
 return { name: app.name, type: app.type }; // output the same
 }
}

In the code above, we didnโ€™t change the body of the Developer class and didnโ€™t create an instance of App, but the result was the same. In this case, we saved a lot of time and code typing.

Conclusion

When should we use classes and interfaces? If you want to create and pass a type-checked class object, you should use TypeScript classes. If you need to work without creating an object, an interface is best for you.

Eventually, we opened two useful approaches: blueprints and contracts. You can use both of them together or just one. It is up to you.

Thanks for reading โ€” I hope you found this piece useful. Happy coding!

LogRocket understands everything users do in your web and mobile apps.

๐Ÿ‘ LogRocket Dashboard Free Trial Banner

LogRocket lets you replay user sessions, eliminating guesswork by showing exactly what users experienced. It captures console logs, errors, network requests, and pixel-perfect DOM recordings โ€” compatible with all frameworks, and with plugins to log additional context from Redux, Vuex, and @ngrx/store.

With Galileo AI, you can instantly identify and explain user struggles with automated monitoring of your entire product experience.

Modernize how you understand your web and mobile apps โ€” start monitoring for free.

๐Ÿ‘ Image
๐Ÿ‘ Image
๐Ÿ‘ Image

Stop guessing about your digital experience with LogRocket

Get started for free

Recent posts:

What is TSRX?: What JSX would look like if it were designed today

TSRX adds first-class control flow, conditional hooks, and scoped styles to React via a TypeScript compiler extension โ€” no new framework required.

๐Ÿ‘ Image
Ikeh Akinyemi
Jun 12, 2026 โ‹… 6 min read

How to add authentication to a React Native app with Better Auth

Learn how to build a full React Native auth system using Better Auth and Expo โ€” with email/password login, Google OAuth, session persistence, and protected routes.

๐Ÿ‘ Image
Chinwike Maduabuchi
Jun 9, 2026 โ‹… 13 min read

AI dev tool power rankings & comparison [June 2026]

Compare the top AI development tools and models of June 2026. View updated rankings, feature breakdowns, and find the best fit for you.

๐Ÿ‘ Image
Chizaram Ken
Jun 8, 2026 โ‹… 11 min read

How to check username availability at scale with Bloom filters

Learn how Bloom filters reduce database lookups for username availability checks while preserving correctness at scale.

๐Ÿ‘ Image
Rosario De Chiara
Jun 8, 2026 โ‹… 6 min read
View all posts

Would you be interested in joining LogRocket's developer community?

Join LogRocketโ€™s Content Advisory Board. Youโ€™ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.

Sign up now