Skip to content

Instantly share code, notes, and snippets.

@KATT
Created June 18, 2019 11:37
Show Gist options
  • Save KATT/35bd6806ed6a86bd23e0a296a4d3d824 to your computer and use it in GitHub Desktop.
Save KATT/35bd6806ed6a86bd23e0a296a4d3d824 to your computer and use it in GitHub Desktop.
WIP
type AnyFunction = (...args: any) => any
type FunctionWithOneArg<T extends AnyFunction> = (args: Record<string, any>) => ReturnType<T>
type Without<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
type Arguments<T> = [T] extends [(...args: infer U) => any]
? U
: [T] extends [void] ? [] : [T]
type PartialArg<T> = Partial<Arguments<T>[0]>
function withDefaults<T extends FunctionWithOneArg<T>>(fn: T, defaults: PartialArg<T>) {
type FunctionOpts = Arguments<typeof myFunction>[0]
type DefaultKeys = keyof typeof defaults
type FunctionDefaults = Pick<Partial<FunctionOpts>, DefaultKeys>
type NewOpts
return (args:) => {
}
}
function myFunction(args: {
foo: string;
bar: string;
}) {
return {foo, bar}
}
type myFunctionArgs = Arguments<typeof myFunction>[0]
const myFunctionWithDefaults = withDefaults(myFunction, {
foo: '1',
})
console.log(myFunctionWithDefaults({
bar: '2',
}))
console.log(myFunctionWithDefaults({
bar: '2',
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment