寻求更好地理解声明外部 javascript 模块(编写 .d.t.s 文件)
Seeking better understanding of declaring external javascript modules (writing .d.t.s files)
经过反复试验,我想出了如何为复杂的 NPM javascript 包编写 .d.ts 文件。该包使用了bluebird promises,所以需要导入bluebird和export bluebird Promise接口。我找到的解决方案归结为如下内容:
/// <reference path='../typings/bluebird/bluebird.d.ts' />
declare module 'other' {
import Promise = require('bluebird');
interface Foo {
func1(): Promise<void>;
}
var Other: Foo;
export = Other;
}
declare module Other {
export module X {
export interface Y {
func2(): Promise<void>;
}
}
}
此文件中没有任何地方 Promise
显式导出,但 Typescript 应用程序可以通过引用路径和 import Other = require('other');
导入此模块,然后使用类型 Promise
,甚至无需限定范围输入 Other.Promise
.
我查看了手册和语言规范,试图更好地理解,但无济于事,尽管我可能遗漏了一些东西。似乎 export = <identifier>;
导出的不仅仅是标识符。谁能赐教一下?
问题在bluebird.d.ts:
declare class Promise<R> implements Promise.Thenable<R>, Promise.Inspection<R>
它在全局范围内声明 class。
Nowhere in this file is Promise explicitly exported, yet Typescript applications can import this module via a reference path and import Other = require('other');
当文件 a
执行 /// <reference foo
之后文件 b
执行 /// <reference a
然后文件 b
隐式地 引用 foo
此处您的文件引用 bluebird
,因此任何 references
您的文件都将隐式引用 bluebird
。
参考与导入
如果文件中没有根级别 import
/ export
,/// <reference
仅 有效。如果文件中有根级别 export
,则它被认为是 外部模块 ,并且只能在另一个文件中使用相应的 import
引入。
经过反复试验,我想出了如何为复杂的 NPM javascript 包编写 .d.ts 文件。该包使用了bluebird promises,所以需要导入bluebird和export bluebird Promise接口。我找到的解决方案归结为如下内容:
/// <reference path='../typings/bluebird/bluebird.d.ts' />
declare module 'other' {
import Promise = require('bluebird');
interface Foo {
func1(): Promise<void>;
}
var Other: Foo;
export = Other;
}
declare module Other {
export module X {
export interface Y {
func2(): Promise<void>;
}
}
}
此文件中没有任何地方 Promise
显式导出,但 Typescript 应用程序可以通过引用路径和 import Other = require('other');
导入此模块,然后使用类型 Promise
,甚至无需限定范围输入 Other.Promise
.
我查看了手册和语言规范,试图更好地理解,但无济于事,尽管我可能遗漏了一些东西。似乎 export = <identifier>;
导出的不仅仅是标识符。谁能赐教一下?
问题在bluebird.d.ts:
declare class Promise<R> implements Promise.Thenable<R>, Promise.Inspection<R>
它在全局范围内声明 class。
Nowhere in this file is Promise explicitly exported, yet Typescript applications can import this module via a reference path and import Other = require('other');
当文件 a
执行 /// <reference foo
之后文件 b
执行 /// <reference a
然后文件 b
隐式地 引用 foo
此处您的文件引用 bluebird
,因此任何 references
您的文件都将隐式引用 bluebird
。
参考与导入
如果文件中没有根级别import
/ export
,/// <reference
仅 有效。如果文件中有根级别 export
,则它被认为是 外部模块 ,并且只能在另一个文件中使用相应的 import
引入。