What’re the Interfaces in TypeScript, and What’s the Difference Between Type Aliases and Interfaces?

1. What’s the Interface?

As JavaScript developers, especially those who write TypeScript, we use the objects to group data and pass it around. In the TypeScript world, there are two ways to achieve this. One of these ways is Interfaces. The Interfaces defines the syntax that any entity must adhere to. In other words, It forces developers to consider the type of properties, methods, and events when they are using it. Interfaces contain the definition of their members.

Interface as a Type

Line of explanations :

  1. We define interfaces using the “interface” keyword and add a name beside the interface keyword.

2/3. We could add properties in the interfaces with their type.

4. If we have an optional property in an interface, we must add the “?” prefix in front of the property name.

5. We also could add methods in the interfaces with its return type.

Difference Between Interfaces and Type Aliases:

  1. Declaration merging doesn’t work with type aliases, while it works properly with interfaces. Let me explain it. You can define the same interface multiple times, so its definitions will be merged into one.
Declaration merging

this usage doesn’t work with type aliases because the type is a unique type unit.

--

--

--

I’m a computer engineering student. I work at a startup as a frontend developer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

https://youtu.be/NegRxsiakcQ

Quick Guide To Using useEffect

How to Compress (Gzip) Angular Build and Deploy to AWS S3 (70% build Optimization)

The many faces of a calendar (part 1)

Snapshots of This Day in History app with calendar stylized as a scroll

Blocknative at ETHDenver: You Had Questions, We’ve Got Answers (And Observations)

Top 10 Angular Material Admin Dashboard Templates

These React Fundamentals You Skip may be Killing You

Exploring React —  Higher Order Components and Redux

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Huseyin Gulcu

Huseyin Gulcu

I’m a computer engineering student. I work at a startup as a frontend developer.

More from Medium

Script Of Typescript

What is Typescript

Summary of high-level built-in types in TypeScript

typescript