Last active
January 9, 2024 10:28
-
-
Save schuhwerk/5af62a2174f0e1e0a84f6ae62083e6d6 to your computer and use it in GitHub Desktop.
Infer types from an object. Make object-key prefixed with "_" partials (not required)
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
// Keys in R overwrite the ones in Default. | |
type Modify<Default, R> = Omit<Default, keyof R> & R | |
// get only types where key is prefixed with Prefix. | |
type FilterPrefixed<O, Prefix extends string> = { | |
[K in keyof O as K extends `${Prefix}${infer _}` ? K : never]: O[K] | |
} | |
/** | |
* Object-Keys that are not prefixed with "_" become partials. | |
* Example: a = { b: 1, _c: 2 }. | |
* type A = _Partial<typeof a> // { b?: number; c: number } | |
*/ | |
type _Partial<O> = Modify<Partial<O>, FilterPrefixed<O, "_">> | |
const a = { b: 1, _c: 2 } | |
type A = _Partial<typeof a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment