[英]How to create TypeScript definition file (d.ts) for umd library
I am working on the library surveyjs 我正在进行图书馆调查
It uses gulp+webpack to build umd bundle. 它使用gulp + webpack来构建umd包。
I want to create the type definition bundle (or may be just multiple d.ts files) for using in typescript projects. 我想创建类型定义包(或者可能只是多个d.ts文件),以便在typescript项目中使用。 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 此处描述了Survey。*的所有争议: 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? 我曾尝试使用: github.com/SitePen/dts-generator和github.com/TypeStrong/dts-bundle,但没有成功,有人可以告诉我正确的方向吗?
You can ask tsc to generate the declaration files for your code by adding the declaration flag in tsconfig.json . 您可以通过在tsconfig.json中添加声明标志,让tsc为您的代码生成声明文件。
In your case it would be: 在你的情况下,它将是:
{
"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"
]
}
As toskv mentioned you can use tsc to generate the .d.ts files. 正如toskv所提到的,您可以使用tsc生成.d.ts文件。 Problem there is that the compiler creates multiple declaration files, one for each of your .ts file.
问题是编译器创建了多个声明文件,每个文件对应一个.ts文件。 I had the same problem with another projected I worked on.
我和另一个投影的人有同样的问题。 I soved it by creating a small plugin for webpack which merges the .d.ts files generated by tsc.
我通过为webpack创建一个小插件来融合它,该插件合并了由tsc生成的.d.ts文件。
You can check it out on here: https://www.npmjs.com/package/typescript-declaration-webpack-plugin 你可以在这里查看: https : //www.npmjs.com/package/typescript-declaration-webpack-plugin
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.