在TypeScript中拥有@ types-style声明

时间:2017-02-14 21:06:40

标签: typescript typescript-typings typescript2.0

我想在assert / browserify个捆绑包中使用tsify模块。请参阅https://www.npmjs.com/package/asserthttps://github.com/substack/browserify-handbook#builtins以及https://nodejs.org/api/assert.html。简而言之,它是浏览器中与node-js兼容的assert模块,即内置浏览器。

所以我在@types/assert添加了package.json。但是声明模块无法识别(似乎有些错误)。所以我想做一个等同于node_modules/@types/assert但在node_modules之外的node_modules不应该在源代码控制中。

有可能吗?如果没有,可以/应该使用/// <reference老派语法或declare module "assert"或什么?

2 个答案:

答案 0 :(得分:0)

声明模块乍一看对我来说没问题(没有彻底的测试),但无论如何,假设你在谈论this library

创建一个将在源代码管理下的typings/assert/assert.d.ts目录/ subdir /文件

必须将assert.d.ts构造为外部模块定义,该定义导出单个符号assert。内容几乎是@ types / assert中找到的内容的直接副本(带有您提到的所需更正)

declare module 'assert' {

    function assert(value:any, message?:string):void;

    namespace assert {

         export function fail(actual?:any, expected?:any, message?:string, operator?:string):void;

         ...

    }

    export = assert

}
tsconfig.json中的

,只需确保编译中包含(即不排除)typings目录的内容。

答案 1 :(得分:0)

澄清@Bruno Grieder的答案。

tsc

中解释.d.ts如何查找declare module 'assert'个文件

puts @image.map { |inner| inner.join ' ' }.join $/ #⇒ 0 0 0 0 # 0 1 0 0 # 0 0 0 1 # 0 0 0 0 的机制称为环境模块:

https://www.typescriptlang.org/docs/handbook/module-resolution.html