在 TypeScript 中拥有自己的@types-style 声明
Own @types-style declaration in TypeScript
我想在 browserify
/tsify
包中使用 assert
模块。参见 https://www.npmjs.com/package/assert and https://github.com/substack/browserify-handbook#builtins and https://nodejs.org/api/assert.html。简而言之,它是浏览器中的 node-js 兼容 assert
模块,即 browserify builtin.
所以我将 @types/assert
添加到我的 package.json
。但是,声明模块无法识别(似乎有问题)。所以我想做一个相当于 node_modules/@types/assert
但在 node_modules
之外的事情,因为 node_modules
不应该在源代码管理中。
可能吗?如果不是,can/should 我使用 /// <reference
老式语法或 declare module "assert"
或什么?
声明模块乍一看对我来说没问题(没有彻底测试)但无论如何,假设你在谈论 this library
创建一个 typings/assert/assert.d.ts
directory/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
目录的内容包含(即不排除)编译。
澄清@Bruno Grieder 的回答。
tsc
如何找到 .d.ts
个文件在 https://www.typescriptlang.org/docs/handbook/module-resolution.html
中有解释
declare module 'assert'
的机制称为环境模块:
https://www.typescriptlang.org/docs/handbook/modules.html#ambient-modules
我想在 browserify
/tsify
包中使用 assert
模块。参见 https://www.npmjs.com/package/assert and https://github.com/substack/browserify-handbook#builtins and https://nodejs.org/api/assert.html。简而言之,它是浏览器中的 node-js 兼容 assert
模块,即 browserify builtin.
所以我将 @types/assert
添加到我的 package.json
。但是,声明模块无法识别(似乎有问题)。所以我想做一个相当于 node_modules/@types/assert
但在 node_modules
之外的事情,因为 node_modules
不应该在源代码管理中。
可能吗?如果不是,can/should 我使用 /// <reference
老式语法或 declare module "assert"
或什么?
声明模块乍一看对我来说没问题(没有彻底测试)但无论如何,假设你在谈论 this library
创建一个 typings/assert/assert.d.ts
directory/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
目录的内容包含(即不排除)编译。
澄清@Bruno Grieder 的回答。
tsc
如何找到 .d.ts
个文件在 https://www.typescriptlang.org/docs/handbook/module-resolution.html
declare module 'assert'
的机制称为环境模块:
https://www.typescriptlang.org/docs/handbook/modules.html#ambient-modules