如何通过@types 为 scoped/namespaced 包安装 TypeScript 声明?

How can I install TypeScript declarations for scoped/namespaced packages via @types?

我正在使用 TypeScript,我想使用一个不附带自己的类型声明的范围包(例如 @foo/bar@babel/core 等)。

我试过运行类似

的东西
npm install @types/@foo/bar

但是好像没有。

有什么方法可以将这些包的 .d.ts 纳入 @types 范围?如果需要,有没有办法在 DefinitelyTyped 上编写我自己的范围包?

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

是的,有办法!就是有点不直观。

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

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

npm install @types/foo__bar

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

是的!来自 Definitely Typed README.md:

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