TypeScript Type vs Interface

Learn via video courses
Topics Covered

Overview

We know that while using typescript some of the developers use types while some user interfaces, so basically let's learn about what are types and interfaces. Interfaces are a way to define data shapes it is the syntax which classes have to follow in the other hand types are defined as those data types of the variables which can be either built-in types or user-defined or any other data types which are pre-defined before declaring any variable that is used as an input in the program.

Introduction

As we already know that the type in typescript is defined for declaring a variables data type while interface is defined as the syntax for the class which provides a way to define entities. Now let's read about where we can use types and interfaces in typescript.

Flexibility

The type or type alias in typescript is defined as the type declaration for creating a variable name with a data type declared before the name where it can create names for a type such as built-in type declaration which includes the numbers, string, boolean values, null, undefined, etc and the type can also declare user-defined types such as union, intersection, and tuple type. We can say that interfaces have more capabilities than types hence it is said that types are more flexible than the interfaces.

Now we will learn the syntax of both typescript type vs interface: Now let's see the syntax for types in typescript:

Let's see the syntax of the interface in typescript:

The syntax for both of them is mostly similar. Now let's read about merging the declarations:

Merging the Declarations

Merging the declarations is only possible with interfaces and not with types. It happens when the typescript compiler merges two or more interfaces that share the same name into only one that is in a single declaration. Let's see an example below:

Here we have created two interfaces with the same name but with different properties but here typescript will automatically merge both of them hence merging the declarations is very useful and it only works in the case of interfaces and not with types because if we create two types with the same names but with different properties typeScript will throw an error. Now let's read about classes.

Classes

We know that in typescript class does not support the feature of implementing or extending union types in typescript hence the type is not used in classes while interfaces in typescript can extend classes, this is a very good concept that helps a lot in a more object-oriented way of programming. Now let's see an example of how to implement it in our program:

In the example above we have a class called colors and an interface called Newcolors that is we can easily extend this class using an interface. Run the above code in your editor for a better explanation.

Primitives

Primitives can only be used by types and not interfaces. Let's see an example below:

Unions

Union types are used to create new types that are it can have a value of one or a few more types. To create a union type, we have to use the | keyword. Now let's see an example below :

Here we have seen an example with types let's see another example of a union type combining two interfaces :

Tuple Types

Tuples are very useful because they can have two sets of values of different data types. We can only declare tuples using types and not interfaces. Let's see an example below:

Though we cannot declare a tuple in TypeScript using an interface, we can still use a tuple inside an interface, like in the example shown below:

This is how we can use a tuple inside an interface. Run the code in your editor for a better explanation.

Intersection

An intersection type is a type that allows us to merge several kinds into one. This allows us to combine many types to create a single type with all of the properties that we require. The & operator is used to combine multiple types using the intersection type but it does not combine multiple types into one interface. Let's see an example below :

Inheritance

Inheritance is the ability to inherit attributes and methods from a parent. Inheritance is only possible with interfaces it is not possible with types. Let's see an example below :

Few Aspects

Types in typescript are used to describe functions, constructors, and tuples but types cannot be implemented or extended in classes interfaces can also be used to describe functions, constructors, and tuples, and also can be implemented and extended in the class and interfaces can also be recursive when compared to types in Typescript.
Now let's see a comparison table difference typescript types and interfaces.

Typescript Types vs Interfaces

Let us read the difference between typescript type vs interface:

TypeInterface
In typescript type is defined for a variable’s data type which means type declaration in typescript is used for declaring names of different types such as user-defined, built-in, or any other data types.Interfaces are defined as the syntax for the class which provides a way to define entities.
It uses the type keyword for defining new types.It uses the interface keyword for declaring an interface.
Types can be used for others like built-in types, tuples, etc.The interface cannot be used with other types of declaration.
Types cannot be extended and hence it does not support classes.Interfaces can be extended and it supports classes.
In Typescript, union types can contain one or more types.In typescript this feature of union types is not supported as we cannot create a union interface by combining two types.

Conclusion

  • We have learned about the different use cases of typescript types and interfaces but we have to implement them according to our use.
  • We can use both typescript type and interface in a program as it is possible and the program may work may fine.
  • We have learned about the use cases of both types and interfaces and how we can apply both of them in real projects.
  • Types and interfaces are very much similar and we have to use them according to our use case.