What is the difference between Types and interfaces in Typescript when we want to use a combination of them?

What is the difference between Types and interfaces in Typescript when we want to use a combination of them?

1- we can extend and implement types using the interface. This is not possible using the type aliases.

interface Animal
  name:string;
}


interface Lion extends Animal{
  isWild:true;
}

2- We can combine multiple types and interface with the"&" keyword into a single type. But, we cannot combine them into a single interface.

type animal= { }
type human= {}


type creature = animal & human

3-Typescript compiler intelligently merges two or more interfaces that share the same name into only one declaration but type aliases can't be changed once a type is created using the type alias

interface Developer{
 numberOfProject: number;
}

interface Developer{
 skills:string[];
}


const AliGhafoori:Developer= {
  numberOfProject: 10,
  skills:['Typescript','Javascript']
}


type developer{
 numberOfProject: number;
}

type developer{
 skills:string[];
}
// Error: Duplicate identifier 'developer'


To view or add a comment, sign in

Others also viewed

Explore topics