如何创建作为全局导出的javascript模块的打字声明?

时间:2017-05-19 18:44:48

标签: javascript typescript webpack typescript-typings

我写了一个用typescript编写的库,我使用webpack将它捆绑到一个缩小的js文件中,该文件将库导出为一个全局变量。我想为我的团队中的开发人员分发这些类型。它们不是打字稿用户,所以他们会通过<script src="...">标签导入库本身,他们会使用三斜杠参考指令/// <reference path="..." />typings或类似的东西导入打字。

问题是:

如何为使用vs代码的js开发人员启用我的库的打字(即intellisense)?我如何声明有一个全局变量导出我输入的值点呢?

我已启用declarationDir and declartion compiler options为我的所有打字稿源文件创建声明文件(*.d.ts),但这些声明并未声明方法中有一个全局变量可用

我尝试在捆绑和缩小index.d.ts文件的同一文件夹中手动创建index.js声明,但我无法使其生效。

以下是我的尝试:

import * as myModule from './typings/src/';

declare module TheGlobalVariable {
    // how do I declare that `TheGlobalVariable` has the same methods
    // as `myModule` exports?
    export = myModule; // doesn't work
}

其中./typings/src/index.d.ts是为此库的入口点生成的声明文件,TheGlobalVariable是全局变量webpack exports的名称。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

哦,我明白了。

在捆绑的js库文件所在的同一文件夹中(index.js),我添加了一个文件index.d.ts。这是该文件:

export * from './src';
export as namespace OntologyStore;

关键促成因素是export as ...

请参阅此module templatethis documentation on library structures

同时将webpack libraryTarget更改为'umd'。 UMD模块检查是否有模块加载器,如果没有,它将导出到全局。此外,我不必使用declarationdeclarationDir编译器选项。

答案 1 :(得分:0)

使用此结构https://github.com/botika/typescript-package。 只遵循README.md

相关问题