Skip to content

Instantly share code, notes, and snippets.

@schuhwerk
Last active January 9, 2024 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schuhwerk/5af62a2174f0e1e0a84f6ae62083e6d6 to your computer and use it in GitHub Desktop.
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)
// 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