typescript multiple types input properties code example

Example 1: typescript union types

type Cow = {
  name: string;
  moo: () => void;
};

type Dog = {
  name: string;
  bark: () => void;
};

type Cat = {
  name: string;
  meow: () => void;
};

// union type
type Animals = Cow | Dog | Cat;

Example 2: typescript interface

interface LabeledValue {
  label: string;
}

function printLabel(labeledObj: LabeledValue) {
  console.log(labeledObj.label);
}

let myObj = { size: 10, label: "Size 10 Object" };
printLabel(myObj);Try