Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading