简体   繁体   English

如何为umd库创建TypeScript定义文件(d.ts)

[英]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-generatorgithub.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.

相关问题 JavaScript库的.d.ts打字稿文件 - .d.ts typescript file for a javascript library 在不编译.js的情况下生成打字稿定义文件(d.ts) - Generating Typescript Definition File (d.ts) without compiling .js 打字稿定义文件d.ts函数的可选属性 - Typescript definition file d.ts function optional properties 重新导出 typescript.d.ts 定义文件中的所有类型 - Reexport all typings inside a typescript .d.ts definition file 如果该库没有可用的类型定义(*.d.ts)文件,如何在 angular4 中导入外部 javascript 库 - how to import external javascript library in angular4 if there is no type definition(*.d.ts) file is available for that library 使用外部模块时,如何在定义文件“ * .d.ts”打字稿中引用类? - How should I reference a class in a definition file “*.d.ts” typescript while using external modules? Q“noConflict()”的TypeScript定义(d.ts) - TypeScript Definition (d.ts) for Q “noConflict()” 将一个打字稿库捆绑到一个.js文件和一个.d.ts - Bundle a typescript library to one .js file and one .d.ts 如何从现有的JavaScript库生成.d.ts“typings”定义文件? - How do you produce a .d.ts “typings” definition file from an existing JavaScript library? 如何将 TypeScript 库编译为.mjs 并包含 d.ts? - How to compile a TypeScript library to .mjs and include d.ts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM