网站搜索

TypeScript d.ts 中的函数对象


我正在尝试将一个库(不是我写的)映射到一个 .d.ts 文件。它有一个对象,这个对象是一个函数,但它也有属性(其中一些也是函数),像这样:

var asd = function () { return 1; };
asd.two = function () { return 2; };
asd.three = 'three';

我如何在 .d.ts 文件中写出 asd 的类型?如何指定它是一个返回数字的函数,并且有两个属性,一个是返回数字的函数,一个是字符串

答案#1

使用声明合并:

declare function asd(): number;

declare module asd {
    function two(): number;
    var three: string;
}

测试:

let num: number = asd();
num = asd.two();
asd.three = "str";

相关文章