jared.fail

TIL Typescript Partial

Today I learned that Typescript's Partial makes all fields optional from the type you are "extending".

I was trying to add a new field to a type from a 3rd party library to correct a mismatch between their types and the actual API response.

type MyAPIResponse = Partial<TheirAPIType> & {email: string};

However, this makes all fields optional, even ones that are required on TheirAPIType.

Turns out, TheirAPIType is an interface, and all I needed was extends.

interface MyAPIResponse extends TheirAPIType {
    email: string;
};