Skip to content

Instantly share code, notes, and snippets.

@esamattis
Created September 17, 2018 19:01
Show Gist options
  • Save esamattis/70e9c780e08937cb0b016e04a7422010 to your computer and use it in GitHub Desktop.
Save esamattis/70e9c780e08937cb0b016e04a7422010 to your computer and use it in GitHub Desktop.
Typescript DeepRequired
type NotNill<T> = T extends null | undefined ? never : T;
type Primitive = undefined | null | boolean | string | number | Function;
type DeepRequired<T> = T extends Primitive
? NotNill<T>
: {
[P in keyof T]-?: T[P] extends Array<infer U>
? Array<DeepRequired<U>>
: T[P] extends ReadonlyArray<infer U2>
? DeepRequired<U2>
: DeepRequired<T[P]>
};
@jokull
Copy link

jokull commented Jun 2, 2023

Nice 🤩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment