what are intersection type? – DEV Community


intersection type are create a new type by extending exciting types using
& operator

interface type1{...}
interface type2{...}
type type3 = type1 & type2
Enter fullscreen mode

Exit fullscreen mode

using & works same way as using extends clause with only difference being
in way they resole conflicts

//these are two interfaces with same property but with incompatible types
interface type1{
  property:string
}
interface type2{
  property:number
}
type3 = type1 & type2
Enter fullscreen mode

Exit fullscreen mode

type3 will result in never type because
TypeScript will try to combine the types regardless

interface type2 extends type1{
  property:number
}
Enter fullscreen mode

Exit fullscreen mode

here typescript will throw an error because the types are incompatible



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *