通过webpack使用typescript defenitions构建js库

时间:2016-08-08 06:53:01

标签: typescript

例如,我的库包含2个用commonjs编写的文件。

输入button.js

button.d.ts

class Button {};

export = Button;
输入index.js

index.d.ts

import Button = require("./button");

export var myButton: typeof Button;

我想生成可由全局命名空间使用的mylib.d.ts。像这样:

let button = new MyLib.Button();

1 个答案:

答案 0 :(得分:0)

尽管typescript支持全局类型声明,但您仍然必须导入该类的实际实现:

// index.d.ts
declare namespace MyLib {
  class Button {
    public name
    constructor(name: string)
  }
}

// index.ts

class theButton {
  public count
  public name
  constructor(count: number) {
    this.count = count
  }
}

let a: MyLib.Button = new theButton(1)
a.name // compiles fine
a.count // compilation error