1

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?

1 Answer 1

1

You have several Typescript errors in your code even before getting to anything related to the Ontology. I'll point out a few and I would suggest fixing those first and then editing your question to be more specific about the ontology edit function type signature.

I recommend using the TypeScript playground as a way of iterating on notional code, especially when working through type signatures. If you prefer an offline REPL experience, Deno is pretty handy. In the event that you or future readers are learning TypeScript for the first time, I highly recommend spending 2h running through the TypeScript tutorial -- it will save you a lot of time compared to learning by working through any examples you see in the Foundry docs since they're all more involved than what is appropriate for TypeScript newcomers (IMO).

First, getting the types of parameters from a function is a very roundabout way of creating a type signature in this situation. Why not just have:

type baseParams {
    a: string;
    b?: Double
}

Second, testFunc1 indicates a misunderstanding of how argument expansion works in JavaScript/TypeScript. In the body of your function a is not defined because the only arguments you're passing in are add and args. The args argument in your example would be equal to ["work", 0] -- i.e. an array that whose elements are not bound to any variable names such as a.

Third, I'm pretty sure you need the @Edit() decorator above your @OntologyEditFunction() decorator.

Again, my suggestion would be to work with a TypeScript REPL, tutorial, and docs to get everything above the @OntologyEditFunction() decorator working and then update your question so there's a more focused chunk of code for folks to assist with.

Sign up to request clarification or add additional context in comments.

1 Comment

I think I may have crossed two questions; I've been using a Typescript playground, but I haven't been able to figure out the code prior to the @OntologyEditFunction(), and so it's actually the main issue I'm asking about. The Ontology part is the next thing to deal with after this is resolved. To better focus the question, I've removed the parts of the question that explicitly relate to Foundry, plus added some clarification and additional information.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.