我想使用 assert
browserify
中的模块/tsify
捆绑。参见 https://www.npmjs.com/package/assert和 https://github.com/substack/browserify-handbook#builtins和 https://nodejs.org/api/assert.html .简而言之,它与 node-js 兼容 assert
浏览器中的模块,即内置的 browserify。
所以我添加了@types/assert
到我的package.json
.但是,声明模块无法识别(似乎有问题)。所以我想做相当于 node_modules/@types/assert
的事情但在 node_modules
之外作为node_modules
不应在源代码管理中。
这可能吗?如果没有,我可以/应该使用 /// <reference
老式语法或 declare module "assert"
或者什么?
最佳答案
声明模块乍一看对我来说没问题(没有彻底测试)但无论如何,假设你在谈论 this library
创建一个 typings/assert/assert.d.ts
目录/subdir/file 将在源代码控制下
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
目录的内容包含(即不排除)编译。
关于typescript - 在 TypeScript 中拥有自己的@types-style 声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42236210/