How to extend types in Typescript

Photo by Kevin Ku
Photo by Kevin Ku

Use an interface (Typescript 2.2+)

We can extend types in Typescript by using the extends keyword For example we can write:

type Profile = {
    name: string,
    email: string,
    created_at: string | Date,
    updated_at: string | Date

interface UserProfile extends User {
    user_id: number

Use an intersection type

type UserProfile = Profile & { user_id: number }

Profile picture

Written by who lives and works in Wisconsin building useful things, and thinks that pineapple on pizza is okay. You should follow them on Twitter