Typescript modifying or extending call signatures
Can one modify a function's call signature as follows:
given fn1:
type fn1 = ( a: string, b: number, c: string )=>void
Psuedo code output required:
type fn2 = ( ...fn1.parameters, d: number )=>void // new type: ( a: string, b: number, c: string, d: number )=>void
//or
type fn2 = ( pick<fn1.parameters,'a','c'>, d: number )=>void // new type: ( a: string, c: string, d: number )=>void
Solution 1:
You can use the type utility Parameters<Type>
to extract parameter information from a function:
TS Playground
type Fn1 = (a: string, b: number, c: string) => void;
type Fn2 = (...params: [...Parameters<Fn1>, number]) => void; // (params_0: string, params_1: number, params_2: string, params_3: number) => void
type Fn3 = (...params: [a: Parameters<Fn1>[0], c: Parameters<Fn1>[2], d: number]) => void; // (a: string, c: string, d: number) => void