Last active
September 26, 2023 01:58
-
-
Save ztrehagem/9209d58acb2e01e1e4f73fa2a72fe42f to your computer and use it in GitHub Desktop.
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
export class KeyPath<T extends string = string> { | |
#_brand!: never; | |
readonly #path: readonly string[]; | |
constructor(path: T) { | |
this.#path = path.split("."); | |
} | |
of<Expected>(object: unknown): Expected { | |
for (const key of this.#path) { | |
if (object == null) { | |
break; | |
} | |
object = (object as Record<string, unknown>)[key]; | |
} | |
return object as Expected; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment