Learn about difference between Type & Interface in Typescript

If you are using typescript, you might use interface & type but if I ask you the difference between them, are you able to answer it ?

At the end of this article, you will be able to answer it during any discussion or interview !

Type

T…


This content originally appeared on DEV Community and was authored by CodeOzz

If you are using typescript, you might use interface & type but if I ask you the difference between them, are you able to answer it ?

At the end of this article, you will be able to answer it during any discussion or interview !

Type

The basic, it allow us to create a new type !

Interface

In contrary to type, interface is restricted to an object type.

With the news release, type and interface are similar but there some differences between them.

Similitude

Object typing

You can define the shape of an object with both, but the syntax is not the same

with interface:

interface A {
  a: number
}

const a: A = { a: 5 }

with type:

type A = {
  a: number
}

const a: A = { a: 5 }

Extend

Both can be extended, and the difference is ... yes the syntax again !

with interface:

interface A {
  a: number
}

interface AB extends A {
  b: number
}

const ab: AB = { a: 5, b: 6 }

with type:

type A = {
  a: number
}

type AB = A & { b: number }

const a: AB = { a: 5, b: 6 }

The difference

What type can do, and what interface cant do

Unlike interface, type can be used for creating new type with union, tuples or can be used to defined primitive type !

type A = string | number // union

type Primitive = string | boolean | number | null | interface | symbol // Create a new type from primitives type

type DataTuple = [number, string] // tuple typing

What interface can do, and what type cant do

A class can implement an interface

interface A {
  a: number
}

class Toto implements A {
   a = 55
}

Interface can be merged in a single interface if there are defined multiple times

interface Toto {
  a: number
}

interface Toto {
  b: number
}
const toto: Toto = {
   a: 55,
   b: 66,
}

Conclusion

As you can see type & interface are very similar but each other have his own dedicated feature !

I personally use interface when I need to type object structure, and I use type when I need to create type from primitive type or when I want to combine other types in one type !


This content originally appeared on DEV Community and was authored by CodeOzz


Print Share Comment Cite Upload Translate Updates
APA

CodeOzz | Sciencx (2021-03-08T23:15:06+00:00) Learn about difference between Type & Interface in Typescript. Retrieved from https://www.scien.cx/2021/03/08/learn-about-difference-between-type-interface-in-typescript/

MLA
" » Learn about difference between Type & Interface in Typescript." CodeOzz | Sciencx - Monday March 8, 2021, https://www.scien.cx/2021/03/08/learn-about-difference-between-type-interface-in-typescript/
HARVARD
CodeOzz | Sciencx Monday March 8, 2021 » Learn about difference between Type & Interface in Typescript., viewed ,<https://www.scien.cx/2021/03/08/learn-about-difference-between-type-interface-in-typescript/>
VANCOUVER
CodeOzz | Sciencx - » Learn about difference between Type & Interface in Typescript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/08/learn-about-difference-between-type-interface-in-typescript/
CHICAGO
" » Learn about difference between Type & Interface in Typescript." CodeOzz | Sciencx - Accessed . https://www.scien.cx/2021/03/08/learn-about-difference-between-type-interface-in-typescript/
IEEE
" » Learn about difference between Type & Interface in Typescript." CodeOzz | Sciencx [Online]. Available: https://www.scien.cx/2021/03/08/learn-about-difference-between-type-interface-in-typescript/. [Accessed: ]
rf:citation
» Learn about difference between Type & Interface in Typescript | CodeOzz | Sciencx | https://www.scien.cx/2021/03/08/learn-about-difference-between-type-interface-in-typescript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.