Created
June 18, 2019 11:37
-
-
Save KATT/35bd6806ed6a86bd23e0a296a4d3d824 to your computer and use it in GitHub Desktop.
WIP
This file contains hidden or 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 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