When use a interface or class in Typescript

TypescriptClassInterfaceStrong Typing

Typescript Problem Overview


I have a simple scenario of a login that requires user's input a email and password in Typescript. I need to create some type to get this strong-typed and send it to the back-end.

Should this be written as:

export interface UserLogin {
  email: string;
  password: string;
}
//OR
export class UserLogin {
  email: string;
  password: string;
}

And how to know when is the scenario to use any of these?

Typescript Solutions


Solution 1 - Typescript

At it's most basic, a class is essentially an object factory (ie. a blueprint of what an object is supposed to look like and then implemented), whereas an interface is a structure used solely for type-checking.

While a class may have initialized properties and methods to help create objects, an interface essentially defines the properties and type an object can have.

In the scenario you described, one would use the interface to set the type for UserLogin.

Solution 2 - Typescript

If you just need to declare a custom type then use interfaces. IMO, there is a very pragmatic reason - they don't get transpiled into JavaScript so the generated code is shorter.

Interfaces won't work if you need to instantiate objects with constructors or use a framework that instantiates and inject them. Then use classes or abstract classes.

Solution 3 - Typescript

Actually both will do the job. I would suggest you to use interfaces though if you just do type-checking as it is specifically designed for that purpose.

> Classes and interfaces are powerful structures that facilitate not just object-oriented programming but also type-checking in TypeScript. A class is a blueprint from which we can create objects that share the same configuration - properties and methods. An interface is a group of related properties and methods that describe an object, but neither provides implementation nor initialisation for them.

From: https://toddmotto.com/classes-vs-interfaces-in-typescript

Solution 4 - Typescript

> send it to the back-end

Sending classes over the wire requires extra effort - if you use JSON content type, your class will come as plain object to your back-end code. See numerous answers for this question telling how to convert JSON back to a class object so it's possible to call its methods.

But since the class in your question has no methods, it does not matter, unless the back-end code will perform runtime checks using instanceof.

To avoid these problems, use interface which has only data members for representing objects for communicating with back-end.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJosbel LunaView Question on Stackoverflow
Solution 1 - TypescriptalephnerdView Answer on Stackoverflow
Solution 2 - TypescriptYakov FainView Answer on Stackoverflow
Solution 3 - TypescriptD. PhiView Answer on Stackoverflow
Solution 4 - TypescriptartemView Answer on Stackoverflow