Last active
October 15, 2025 19:50
-
-
Save gomezcabo/dff1d95fd1eb354f686d6606a511d7da to your computer and use it in GitHub Desktop.
Typescript RecursiveRequired generic type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' | |
| } | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I guess when passing down the
keyof Tto the generic, this one could bestring,numberorsymboland that's where the Type was failing. No way to determine which one of those. That's why the fix was to include the checkT[K] extends PropertyKey(PropertyKey === string | number | symbol).