Menu

TypeScript: Do you know the differences between type and interface?
📰

TypeScript: Do you know the differences between type and interface?

DEV Community: angular·Ravi Anand·2 days ago
#zAAcjRyo
#devtoangular#devto#dev#javascript#angular
Long article (903 words) — no AI summary yet.
Reading 0:00
15s threshold

They look almost identical, but there are some important differences. Let's explore when to use each.

When defining the shape of an object in TypeScript, we generally have two options: type and interface .

Let's start with a simple example:

type Person = { name : string ; age : number ; } ;

// Using an interface interface Person { name : string ; age : number ; }

Enter fullscreen mode

Exit fullscreen mode

Both approaches describe an object with two properties: name and age .

A quick note: You might see developers using prefixes such as I for interfaces and T for type aliases. These are personal naming conventions, not TypeScript requirements. Generally, avoid unnecessary prefixes.

Another option is class , but unlike type and interface , a class is a JavaScript runtime construct that introduces both a value and a type.

1.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.