Search Terms
type of function's parameters list; type of arguments list; tuples;
Suggestion
There are functions a() and b(). Function a() have the same signature (parameters) as function b().
Instead of manually copying list of parameters with their types from b() to a(), there should be a way to assign types of parameters to a() from b().
functiona(...args: /* possible syntax starts */parametersofb/*possible syntax ends*/){b(...args)}functionb(a: string,b: number){/* ... */}a('foo',1)// oka('foo','bar')// compiler error "Argument of type 'bar' is not assignable to parameter of type 'number'"parametersof ... should probably return a tuple.
Related question on Stackoverflow
Use Cases
- Decorators or simply reusing existing functions:
functiona(...args: parametersofb){// ... do some stuff b(...args)}- API that provides lazy-loaded functions or provides functions based on environment:
importsomeFuncfrom'./someFunc'exportconstsomeAPI={
someFunc,// normal one, we just re-exporting this funcasynclazyFunc(...args: parametersofeagerFunc){const{eagerFunc}=awaitimport('./eagerFunc')returneagerFunc(...args)},// lazy func. helps to reduce main bundlegetnode(){if(runningInNode()){return{asyncnodeFunc(...args: parametersofnodeFunc){const{nodeFunc}=awaitimport('./nodeFunc')returnnodeFunc(...args)}}}}// here we do not add code, that is supposed to work only in node.js, to frontend bundle)}Current workarounds and their faults
functiona(a: string,b: number){b(...args)}functionb(a: string,b: number){/*...*/}Above practice have these disadvantages:
- When changing
b's signature, we also have to change a's one. That is little bit harder maintenance and a window for new errors. - Contradicts with DRY principle
Comparison to tuples
I am aware of new "tuples" feature in TS 3.0, which is great! However using tuples for this use case requires creating new types, while "parametersof" solution will end up in minimum possible amount of code and maximum readability.
Checklist
My suggestion meets these guidelines:
Search Terms
type of function's parameters list; type of arguments list; tuples;
Suggestion
There are functions
a()andb(). Functiona()have the same signature (parameters) as functionb().Instead of manually copying list of parameters with their types from
b()toa(), there should be a way to assign types of parameters toa()fromb().parametersof ...should probably return a tuple.Related question on Stackoverflow
Use Cases
Current workarounds and their faults
Above practice have these disadvantages:
b's signature, we also have to changea's one. That is little bit harder maintenance and a window for new errors.Comparison to tuples
I am aware of new "tuples" feature in TS 3.0, which is great! However using tuples for this use case requires creating new types, while "parametersof" solution will end up in minimum possible amount of code and maximum readability.
Checklist
My suggestion meets these guidelines: