🤔 requirements
Implement a paradigm ChangeArgument
any[]>, for the given function type Fn, returns a new function G.
The parameter type of G is Fn minus the First item, and the return type of G is Fn plus the value of type First at the end
Type Fn = (a: number, b: string) => [number] type Result = ChangeArgument<Fn> Number) => [number, string] type Fn = () => [number] type Result = ChangeArgument<Fn>Copy the code
📖 knowledge
infer
It first appeared herePRIn, indicates inextends
The type variable to be inferred in a conditional statement.- Tuple types allow you to express an array with a fixed number of elements whose types are known, but need not be the same. For example, you may want to represent a value as a pair of a
string
and anumber
: - TypeScript, like JavaScript, allows you to work with arrays of values. Array types can be written in one of two ways. In the first, you use the type of the elements followed by
[]
to denote an array of that element type: - When a rest parameter has a tuple type, the tuple type is expanded into a sequence of discrete parameters. For example the following two declarations are equivalent:
- When a function call includes a spread expression of a tuple type as the last argument, the spread expression corresponds to a sequence of discrete arguments of the tuple element types.
🔗 Knowledge link
- Infer:
- Tuple and Array
- Rest Parameters and Spread expression
- Get the array length
😢 questions & Answers
- Change-Argument
- answer
🍗 scenario
type FirstArgument<T extends (... args: any[]) => any> = Parameters<T>[0] type RestArgument<T extends (... args: any[]) => any> = Parameters<T> extends [ first: any, ...rest: infer R ] ? R : []; const bindFunction = <T extends (... args: any[]) => any,>(fn: T, firstArgs: FirstArgument<T>) => { return (... restArgs: RestArgument<T>) => { fn(firstArgs, ... restArgs) } } const demo = bindFunction((name: string, age: number) => { console.log(name + age)}, "123") demo(123)Copy the code