Created
December 7, 2022 04:04
-
-
Save angeloashmore/c9b0bc643a700164581222441c2225d3 to your computer and use it in GitHub Desktop.
`OmitNested` - A TypeScript type that omits nested object properties using dot notion.
This file contains 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 AppendDotPathSegment< | |
TPath extends string, | |
TSegment extends string, | |
> = TPath extends "" ? TSegment : `${TPath}.${TSegment}`; | |
type AnyFunction = (...args: any[]) => any; | |
type Primitives = | |
| boolean | |
| string | |
| number | |
| symbol | |
| unknown[] | |
| null | |
| undefined | |
| void | |
| AnyFunction; | |
type AllObjDotPaths<TObj, TPath extends string = ""> = TObj extends Primitives | |
? TPath | |
: { | |
[P in keyof TObj]: P extends string | |
? | |
| AppendDotPathSegment<TPath, P> | |
| AllObjDotPaths<TObj[P], AppendDotPathSegment<TPath, P>> | |
: TPath; | |
}[keyof TObj]; | |
type RecursiveOmitNested< | |
TObj, | |
TOmitPath extends string, | |
TPath extends string = "", | |
> = TObj extends Primitives | |
? TObj | |
: { | |
[P in keyof TObj as P extends string | |
? AppendDotPathSegment<TPath, P> extends TOmitPath | |
? never | |
: P | |
: never]: P extends string | |
? RecursiveOmitNested< | |
TObj[P], | |
TOmitPath, | |
AppendDotPathSegment<TPath, P> | |
> | |
: TObj[P]; | |
}; | |
export type OmitNested< | |
TObj, | |
TOmitPath extends AllObjDotPaths<TObj>, | |
> = RecursiveOmitNested<TObj, TOmitPath>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment