I'm writingworking with two OntologyEditFunctions in support of one Object Type - a function to back the main Action to create Objects, and a function to back the main Modify Action.
This Object Type has(and later potentially more) functions that each have upwards of 50 propertiesparameters, eachthe vast majority of which I want my functions to take as inputs. Additionally, for a couple of fields, I take input in one type and transform it into another before storing it (e.g. take a string that can contain a number, then extract the number and store it as a Double)are identical. This means I'm going to haveSo at least two function signatures like:
@OntologyEditFunction()
public myFunc(
a: string,
b: string,
c?: Doublenumber,
.....
cmcy?: Timestampboolean,
cncz?: LocalDateDate,
coda?: string,
db?: boolean,
.....
//And on and on into something resembling infinity
): void {}
...where all but maybe three or four parameters are identical across the two (and probably eventually more) signatures. Note that the parameter names in the real functions are things like username, userId, dateLastEdited, etc.
So I'd likeI want to separately define the parameters that don't change (if not all of them), and then reuse that definition in the OntologyEditFunctionsfunction 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):
import { OntologyEditFunction, Double, Timestamp }type frombaseParams '@foundry/functions-api';
function= baseFunc([
a: string,
b?: Double
)
type baseParams//I =need Parameters<typeofto baseFunc>;capture that this parameter is optional
];
function testFunc1(add?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, addoptionalParam?: string]): string {
a = 'Maybe this time?';
return a; //Not a chance
}
let test2: string = testFunc2('No', 0, 'dice')
exportSo, 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 classbaseParams myClass= {
@OntologyEditFunction()a: //kstring,
and q below wereb?: notDouble included//I inneed theto baseFunc
capture that this parameter publicis createRecordoptional
}
function testFunc3(k?requiredParam: string, q?args: stringbaseParams, ...argsoptionalParam?: baseParamsstring): voidstring {
let fooargs['a'] = a;
'Oh boy does it }
work?';
@OntologyEditFunction()
return args['a'];
}
let publictest1: modifyRecord<Tstring extends= baseParams>testFunc1(...args: [...T'Please', k?{a: Double'work'}, q?: Timestamp]0): void {
let foo = a;
}
}
So, testFunc2 and modifyRecord fail at a minimum because the parameters lose their namesThis returns without issue, which I explicitly want to reuse.
testFunc1 and createRecord each show a pretty encouraging call signature in the tooltip when I hover over their symbols:
function testFunc1(add?: string, a: string, b?: number | undefined): stringfunction createRecord(k?: string, q?: string, a: string, b?: number | undefined): void
...but despitebut it also doesn't work for my needs; this, in the body of each function, I get a compiler error saying it "Cannot find name 'a'." I was surprised when the call signatures appeared is meant to work, then less surprised when the code failed anywaybe called by an automated process that can't handle objects as parameters.
The immediate question is: What did I do wrong withSo. Instead of flooding my attempts abovedocument with 80,000 repeated parameters and can they be mademaking it near impossible to work?
The more pressing question overall is: Howkeep track of where they differ, how can I reuse a set of named &, typed parameters inwithin multiple function calls, instead of flooding my document with 80call signatures,000 repeated parameters? Or is there another way to condense the code and avoid repetition?
And for major bonus brownie points: Is there a method such that also works for Typescript object definitions, so Ithose signatures can use a construct built from the same definition to help assign to the created/modified Objectbe expanded with additional parameters? Something like:
const cols: Partial<myObjectType> = {
...baseParamsMod,
k: k,
q: q
}
const newRecord = Objects.create().myObjectType(id);
Object.assign(newRecord, cols);