【发布时间】:2017-11-24 00:22:23
【问题描述】:
假设我们有一个接受可变数量参数的函数,我相信它被称为可变参数函数。
类似这样的:
function foo(){
let args = Array.from(arguments).map(v => whatever);
// do something with args
}
此函数的 TypeScript 接口可能如下所示:
interface IVariadicFn {
(...args: string[]): void,
}
但是假设我们期望一个可变数量的字符串,但是最后一个参数,我们期望是一个回调函数。
所以我们会有这样的东西:
function variadic(){
let args = Array.from(arguments)
let fn = args.pop();
let $args = args.map(v => whatever);
// do something with args
process.nextTick(function(){
fn(null, $args); // fire the callback, asynchronously
});
}
有没有办法为这个函数声明一个 TypeScript 定义?
我现在能做的最好的就是:
type IVariadicArgs = string | Function;
export interface IBeforeFn {
(...args: IVariadicArgs[]): void;
}
【问题讨论】:
-
讨论目前似乎已经转移到#5453。
-
这个关于静态检查虚假参数/空字符串的问题真的很好
标签: javascript node.js typescript typescript2.0 typescript2.4