Representing UNO out parameters in definitions

79 views Asked by At

The UNO API supports inout and out parameters -- a variable is passed into a function, the function modifies the variable, and the modified variable is then available outside the function.

Javascript doesn't support this type of parameter call, so the Automation bridge enables a workaround -- passing in an array; the element at index 0 is modified, and can be retrieved outside the function:

// for inout parameters
let inout = ['input value'];
obj.func(inout);
console.log(inout[0]); //could print 'new value'

For inout parameters, since an initial value is expected, the function could be declared with a tuple type:

declare interface Obj {
    func(inout: [string]): void;
}

However, an out parameter doesn't need an initial value:

// Javascript

let out = [];
obj.func1(out);
console.log(out[0]); // could print 'new value'

AFAIK there is no way to represent the empty tuple type, and an empty array is incompatible with the tuple type. If I declare the function argument with a tuple type, I cannot pass in an empty array:

// Typescript

declare interface Obj {
    func1(out: [string]): void;
}
let out = [];
obj.func1(out); 
// Argument of type 'any[]' is not assignable to parameter of type '[string]'.
//  Property '0' is missing in type 'any[]'.

link

There are two hacks I could use:

  1. either subvert the type system using any:

    let out: [string] = [] as any;
    
  2. or use an initialized array:

    let out: [string] = [''];
    

Is there any less "hacky" way to do this?

2

There are 2 answers

0
Shane On

You can declare the inout parameter as inout: string[], which enables you to pass on an empty array.

e.g.:

declare interface Obj {
    func(inout: string[]): void;
};

const Obj: Obj = {
    func(inout: string[]) {
        // ...
    }
}

let arr = [];
Obj.func(arr);
2
Daniel Rosenwasser On

Until TypeScript allows empty tuples, you could consider declaring the parameter type as undefined[].