typescript - 在 TypeScript 中拥有自己的@types-style 声明

标签 typescript typescript-typings typescript2.0

我想使用 assert browserify 中的模块/tsify捆绑。参见 https://www.npmjs.com/package/asserthttps://github.com/substack/browserify-handbook#builtinshttps://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/

相关文章:

typescript - 我怎样才能有一个使用对象作为键的字典?

javascript - typescript 打字 - 重复名称 promise /找不到名称 promise

TypeScript 定义文件找不到 .d.ts

javascript - 检索在组件的选择框中选择的值

javascript - 使用类模型/ typescript 时检索属性指令中的参数值?

typescript - 如何在 typescript 中使用 "fill in"泛型参数

javascript - JQueryStatic 类型上不存在属性

typescript - 如何在 typescript 中将对象的值称为泛型?

javascript - Angular 7通过调用两次服务订阅方法进行通信

Typescript 将元组的类型元组转换为元组(扁平元组)