如何通过@types 为作用域/命名空间包安装 TypeScript 声明? [英] How can I install TypeScript declarations for scoped/namespaced packages via @types?

查看:67
本文介绍了如何通过@types 为作用域/命名空间包安装 TypeScript 声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 TypeScript 并且我想使用一个不发货的范围包(例如 @foo/bar@babel/core 等)它自己的类型声明.

我试过运行类似的东西

npm install @types/@foo/bar

但它似乎不可用.

有什么方法可以将这些包的这些 .d.ts 放入 @types 范围?如果需要,有没有办法在绝对类型上编写我自己的作用域包?

解决方案

有什么办法可以将这些包的这些 .d.ts 放到 @types 范围内?

是的,有办法!只是有点不直观.

npm 不允许作用域包在其名称中包含 @,因此这些名称被修改为使用两个下划线代替 @.

举个例子,如果你想为包@foo/bar安装类型声明,你需要运行

npm install @types/foo__bar

<小时><块引用>

如果需要,有没有办法在绝对类型上编写我自己的作用域包?

是的!来自 绝对类型 README:.md><块引用>

作用域包的类型 @foo/bar 应该放在 types/foo__bar 中.注意双下划线.

I'm using TypeScript and I want to use a scoped package (e.g. @foo/bar, @babel/core, etc.) that doesn't ship its own type declarations.

I've tried to run something like

npm install @types/@foo/bar

but it doesn't seem to be available.

Is there any way to get these .d.ts for these packages into the @types scope? Is there a way to write my own scoped packages on DefinitelyTyped if I need to?

解决方案

Is there any way to get these .d.ts for these packages into the @types scope?

Yes there is a way! It's just a little unintuitive.

npm doesn't permit scoped packages to contain @ in their name, so these names are mangled to use two underscores in place of the @.

So as an example, if you want to install type declarations for the package @foo/bar, you'll need to run

npm install @types/foo__bar


Is there a way to write my own scoped packages on DefinitelyTyped if I need to?

Yes! From the Definitely Typed README.md:

Types for a scoped package @foo/bar should go in types/foo__bar. Note the double underscore.

这篇关于如何通过@types 为作用域/命名空间包安装 TypeScript 声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆