I'm working with two (and later potentially more) functions that each have upwards of 50 parameters, the vast majority of which are identical. So at least two function signatures like:
public myFunc(
a: string,
b: string,
c?: number,
.....
cy?: boolean,
cz?: Date,
da?: string,
db?: boolean,
.....
//And on and on into something resembling infinity
): void {}
...where all but maybe three or four parameters are identical across the signatures. Note that the parameter names in the real functions are things like username, userId, dateLastEdited, etc.
I want to separately define the parameters that don't change, and then reuse that definition in the function implementation signatures.
Essentially, I want the function signatures to read like "Use this set of pre-defined named parameters, and also here's a few extra parameters unique to this function." Aside from condensing the code, this would make it significantly easier to keep track of how the parameters differ.
Note, many of the parameters that I want to pre-define are optional and must be captured as such.
Based on this answer to another question, I tried these (updated based on feedback from @Ontologize to help focus the question):
type baseParams = [
a: string,
b?: Double //I need to capture that this parameter is optional
];
function testFunc1(requiredParam: string, ...args: baseParams, optionalParam?: string): string {
a = 'Oh boy it works';
return a; //...Or not
}
let test1: string = testFunc1('Please', 'work', 0)
function testFunc2<T extends baseParams>(...args: [requiredParam: string, ...T, optionalParam?: string]): string {
a = 'Maybe this time?';
return a; //Not a chance
}
let test2: string = testFunc2('No', 0, 'dice')
So, these functions fail for several reasons, including:
- During argument expansion, the parameters lose their names, which I explicitly want to reuse.
- In testFunc1, I'm trying to put an optional parameter after a rest parameter, which of course is not allowed. I'm not sure how else to add optional parameters unique to that function, though, given that the rest parameter needs to include required parameters (which I'm pretty sure is also not allowed).
There's more, but my takeaway is that rest parameters will not work for my needs.
Edit: I did also see an answer (that I've since lost track of) where they achieved named parameters by passing them in as an object, like:
type baseParams = {
a: string,
b?: Double //I need to capture that this parameter is optional
}
function testFunc3(requiredParam: string, args: baseParams, optionalParam?: string): string {
args['a'] = 'Oh boy does it work?';
return args['a'];
}
let test1: string = testFunc1('Please', {a: 'work'}, 0)
This returns without issue, but it also doesn't work for my needs; this function is meant to be called by an automated process that can't handle objects as parameters.
So. Instead of flooding my document with 80,000 repeated parameters and making it near impossible to keep track of where they differ, how can I reuse a set of named, typed parameters within multiple call signatures, such that those signatures can be expanded with additional parameters?