在定义中表示UNO输出参数

时间:2017-09-07 06:27:23

标签: typescript libreoffice uno

UNO API支持inoutout参数 - 将变量传递给函数,函数修改变量,然后修改后的变量在函数外部可用。

Javascript不支持这种类型的参数调用,因此自动化桥enables a workaround - 传入一个数组;索引0处的元素已被修改,可以在函数外部检索:

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

对于inout参数,由于需要初始值,因此可以使用元组类型声明该函数:

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

但是,out参数不需要初始值:

// Javascript

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

AFAIK无法表示空元组类型,而空数组与元组类型不兼容。如果我用元组类型声明函数参数,我不能传入一个空数组:

// 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

我可以使用两种黑客:

  1. 使用any来破坏类型系统:

    let out: [string] = [] as any;
    
  2. 或使用初始化数组:

    let out: [string] = [''];
    
  3. 有没有更少的“hacky”方法吗?

2 个答案:

答案 0 :(得分:1)

您可以将inout参数声明为inout: string[],这样您就可以传递一个空数组。

e.g:

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

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

let arr = [];
Obj.func(arr);

答案 1 :(得分:1)

Until TypeScript allows empty tuples,您可以考虑将参数类型声明为undefined[]