Skip to content

Instantly share code, notes, and snippets.

@gomezcabo
Last active October 15, 2025 19:50
Show Gist options
  • Select an option

  • Save gomezcabo/dff1d95fd1eb354f686d6606a511d7da to your computer and use it in GitHub Desktop.

Select an option

Save gomezcabo/dff1d95fd1eb354f686d6606a511d7da to your computer and use it in GitHub Desktop.
Typescript RecursiveRequired generic type
type RecursiveRequired<T> = Required<{
[P in keyof T]: T[P] extends object | undefined ? RecursiveRequired<Required<T[P]>> : T[P];
}>;
type ExampleType = {
a?: number;
b: number;
c?: {
d?: {
e?: number;
f: boolean;
g?: {
h: string
}
}
}
}
type ExampleTypeRequired = RecursiveRequired<ExampleType>
const data: ExampleTypeRequired = {
a: 1,
b: 1,
c: {
d: {
e: 1,
f: false,
g: {
h: 'hello'
}
}
}
}
@heyzling

heyzling commented Mar 2, 2024

Copy link
Copy Markdown

This also works with arrays, which comes in handy in my case.

type ExampleType = {
  a: number;
  b: {name:string}[]
}

type ExampleTypeRequired = RecursiveRequired<ExampleType>

declare const exampleData: ExampleTypeRequired;

exampleData.b[0].name // always: string, no warnings

Thanks for the gist!

@gomezcabo

Copy link
Copy Markdown
Author

You’re welcome! ☺️

@Sans3108

Copy link
Copy Markdown

appreciate it

@gomezcabo

Copy link
Copy Markdown
Author

Thanks! 🤗

@rambo-panda

rambo-panda commented Apr 19, 2024

Copy link
Copy Markdown

thanks for your gist!!!

Inspired by you, implemented using the -? operator.

type RequiredDeep<T> = {
  [P in keyof T]-?: T[P] extends object | undefined ? RequiredDeep<T[P]> : T[P];
};

@kennarddh

Copy link
Copy Markdown

Supports function

export type DeepRequired<T> = T extends Function
	? T
	: T extends object
		? {
				[P in keyof T]-?: DeepRequired<T[P]>
			}
		: T

@diragb

diragb commented Sep 7, 2024

Copy link
Copy Markdown

was looking for this, thanks @gomezcabo!

@C-Duxbury

C-Duxbury commented May 8, 2025

Copy link
Copy Markdown

@kennarddh Is there any way to make this work with a generic type that references its own type? For example:

interface TableColumn<T> {
    mappedProperty: keyof T;
    label?: string;
}

This interface could represent a table column definition that requires the name of a valid property from the row item schema. Currently, this produces a type that makes mappedProperty of type DeepRequired<keyof T> when applied to TableColumn<T> instead of leaving it as keyof T.

This creates a problem when attempting to create objects that satisfy this in a mapping function:

function normalizeDefs<T extends object>(defs: TableColumn<T>[]): DeepRequired<TableColumn<T>>[] {
    return defs.map(d => ({
        label: d.mappedProperty.toString().toUpperCase(),
        ...d
    }));
}

image

TS playground link.

@gomezcabo

gomezcabo commented May 9, 2025

Copy link
Copy Markdown
Author

Good catch! What about updating the DeepRequired type?

type DeepRequired<T> = T extends Function
  ? T
  : T extends object
    ? Required<{
        [K in keyof T]-?: T[K] extends PropertyKey ? T[K] : DeepRequired<T[K]>;
      }>
    : T;

Now, if T[K] is not an object, but a PropertyKey (e.g. string | number | symbol) we use that type (keyof T in your case).

What do you think @C-Duxbury ?

Link to playground here.

@C-Duxbury

C-Duxbury commented May 9, 2025

Copy link
Copy Markdown

@gomezcabo Yes, that does the trick nicely! Thanks!

Just for my own academic curiosity, do you know why the TS compiler was saying 'string' is not assignable to type 'DeepRequired<keyof T>'? It seems like they should be compatible.

@gomezcabo

Copy link
Copy Markdown
Author

@gomezcabo Yes, that does the trick nicely! Thanks!

Just for my own academic curiosity, do you know why the TS compiler was saying 'string' is not assignable to type 'DeepRequired<keyof T>'? It seems like they should be compatible.

I guess when passing down the keyof T to the generic, this one could be string, number or symbol and that's where the Type was failing. No way to determine which one of those. That's why the fix was to include the check T[K] extends PropertyKey (PropertyKey === string | number | symbol).

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