Modifying inferred type of deeply nested object

Hi folks, Say I have a deeply nested object that I'm inferring types of. type MyObj = typeof myObj But I'm not happy with some of the inferred types. For example (I know...🙄 ) I have a type that is undefined but needs to be of type undefined | boolean. How can I easily modify that nested type? And do it many times with different nested values?
2 Replies
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
benten
bentenOP•3y ago
hey, thanks for the help I think I got it here
type _Overwrite<T, U> = U extends object ? (
{ [K in keyof T]: K extends keyof U ? _Overwrite<T[K], U[K]> : T[K] } & U
) : U

type ExpandRecursively<T> = T extends Function ? T : T extends object
? T extends infer O ? { [K in keyof O]: ExpandRecursively<O[K]> } : never
: T;

export type Overwrite<T, U> = ExpandRecursively<_Overwrite<T, U>>
type _Overwrite<T, U> = U extends object ? (
{ [K in keyof T]: K extends keyof U ? _Overwrite<T[K], U[K]> : T[K] } & U
) : U

type ExpandRecursively<T> = T extends Function ? T : T extends object
? T extends infer O ? { [K in keyof O]: ExpandRecursively<O[K]> } : never
: T;

export type Overwrite<T, U> = ExpandRecursively<_Overwrite<T, U>>
Want results from more Discord servers?
Add your server