How to create TypeScript definition file (d.ts) for umd library

时间:2016-10-20 12:55:24

标签: typescript webpack definitelytyped umd

I am working on the library surveyjs

It uses gulp+webpack to build umd bundle.

I want to create the type definition bundle (or may be just multiple d.ts files) for using in typescript projects. I would like to have something like that:

import * as Survey from 'surveyjs';

All contens for Survey.* is described here: https://github.com/dmitrykurmanov/surveyjs/blob/master/src/entries/ko.ts

I have tried to use: github.com/SitePen/dts-generator and github.com/TypeStrong/dts-bundle but whithout success, could somebody please show me the right direction?

2 个答案:

答案 0 :(得分:5)

您可以通过在 tsconfig.json 中添加声明标记,让 tsc 为您的代码生成声明文件。

在你的情况下,它将是:

{
  "compilerOptions": {
    "target": "es5",
    "module": "es2015",
    "sourceMap": true,
    "noImplicitAny": false,
    "jsx": "react",
    "declaration": true
  },
//  "filesGlob": [
  //    "typings/index.d.ts"
  //  ], // TODO
  "include": [
    "typings/index.d.ts",
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ]
}

答案 1 :(得分:0)

如toskv所述,您可以使用 tsc 生成.d.ts文件。问题在于,编译器会创建多个声明文件,每个.ts文件一个。我在另一个项目上也遇到了同样的问题。我通过为Webpack创建一个小的插件来进行整理,该插件可以合并由tsc生成的 .d.ts 文件。

您可以在此处查看:https://www.npmjs.com/package/typescript-declaration-webpack-plugin