Skip to main content

Command Palette

Search for a command to run...

Type vs Interface in Typescript

Published
1 min read
Type vs Interface in Typescript
O

Hi there 👋🏾. I'm a software engineer that enjoys building stuff and talking about them. I also tinker a bit with hardware and robotics using Arduino and ROS.

Typescript offers two ways to define types. Either Type-Aliases or Interfaces. The choice of which to use depends on the complexity of the type you want to define.

For most cases, a Type-Alias would get the job done. Most things offered by interfaces are also available in the Type-Alias API. Let's see an example

Extending types

With interfaces, you could do this

interface Human {
  height: number,
  age: number,
  name: string
}

interface Woman extends Human {
  canConcieve: boolean
}

Applying the same logic using Type-Aliases would be

type Human = {
  height: number,
  age: number,
  hairColor: string
}

type Woman = {
  canConcieve: boolean
}

const Chisom: Human & Woman = {
  height: 1.5,
  age: 20,
  hairColor: 'brown',
  canConcieve: true
}

People who prefer functional-style programming to Object-Oriented Programming may find Type-Aliases to be a better approach. Loosely defined types that can be combined.

L

Good explanation about it. Thanks for sharing!

More from this blog

O

Osinachi's base

66 posts

Hi there, I'm a software engineer that enjoys building stuff and talking about them. I also tinker a bit with hardware and robotics using Arduino and ROS.