DEV Community

Discussion on: Types vs. Interfaces in Typescript

Collapse
 
peerreynders profile image
peerreynders

See Naming of TypeScript's union and intersection types:

type Dog = {
  breed: string;
};

// Intersection
type Bulldog = {
  isCute: boolean;
} & Dog;

const winston: Bulldog = {
  breed: 'Bulldog',
  isCute: false,
};

// Union
type Pet = {
  name: string
} | Dog;

const cat: Pet = {
  name: 'Fluffykins'
};

const fido: Pet = {
  name: 'froufrou',
  breed: 'Maltese'
};

const dog: Pet = {
  breed: 'Boston Terrier'
}
Enter fullscreen mode Exit fullscreen mode

intersection of types (&): the shape of the data has to conform to the constraints of all the intersected types simultaneously.

union of types (|): the shape of the data has to conform to the constraints of at least one of the unioned types.

Collapse
 
harisharaju1 profile image
Harish Raju

thank you for explaining along with code snippets.....appreciate it!!

Collapse
 
adamellsworth profile image
Adam

isCute: false, but Winston IS cute :(
(lol just being silly. Thank you for the further clarification/doc resrouce)