typescript combine types 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: merge two types typescript

interface IStudent {
    id: string;
    age: number;
}

interface IWorker {
    companyId: string;
}

type IStudentAlias = IStudent;

type ICustomType = IStudent | IWorker;

let s: IStudentAlias = {
    id: 'ID3241',
    age: 2
};

Example 3: typescript assign two types

// refered to as union types in typescript
interface Foo {
    bar:string|boolean;
}

Example 4: typescript utility types merge interfaces

interface A {
    x: string
}

interface B extends Omit<A, 'x'> {
  x: number
}