I have a typescript function which has "Object set A" as input parameter. I want to use the same function but instead of object A I want to pass "Object set B" as input with out having a repeated logic for other object. Is this possible?
I didn't found a way to do this in typescript. Sample example is below. In below example if I want to use that function in quiver or workshop I can only able to pass Object set A as input parameter. Now I have an Object B which I want to pass to same function (Which is not possible here). So is there any way write a generic function that can take Object A or Object B.
public async add(Object set A): Promise<Object> {
........
........
return Object set
or can I write a typescript function that takes multiple objects as input parameters and do an if else condition to work with other objects. Code sample as follows:
public async add(multiple objects): Promise<Object> {
if typeOf(input parameter obj A) === something{
.......do soemthing
}
and so on
As of today, you can't register a function with a parameter typed with multiple object types (e.g. union of Object Types or "OR" condition of Object Types).
You can still use private functions that can handle multiple types, which then you can call from one function for each Object Type you want to support:
import { Function, Integer } from "@foundry/functions-api";
import { Objects, ObjectSet, objecTypeA, objecTypeB } from "@foundry/ontology-api";
export class MyFunctions {
private async exampleMultiTypeFunction<T extends objecTypeA | objecTypeB>(objs: ObjectSet<T>): Promise<Integer> {
const nb_objects = await objs.count() || 0;
return nb_objects;
}
@Function()
public async myFunction_typeA(objs: ObjectSet<objecTypeA>): Promise<Integer> {
return this.exampleMultiTypeFunction(objs);
}
@Function()
public async myFunction_typeB(objs: ObjectSet<objecTypeB>): Promise<Integer> {
return this.exampleMultiTypeFunction(objs);
}
}
This will produce: