安装 npm 包时自动安装类型定义 [英] Install type definitions automatically when I install an npm package
问题描述
有没有办法以这样的方式配置 npm
,这样每当我安装一个包时,它就会:
Is there a way to configure npm
in such a way so that whenever I install a package, it will:
- 检查里面是否有类型定义
- 如果没有,请尝试使用
--save-dev
标志安装@types/PACKAGE
- Check if it has type definitions inside it
- If it does not, try to install
@types/PACKAGE
with the--save-dev
flag
理想情况下,我希望这会自动发生(作为插件或其他东西),而无需编写限制 API 的 shell 脚本.例如,我可能会编写一个 shell 脚本,例如:(注意,这并不能真正满足所有要求)
Ideally, I'd like this to happen automatically (as a plugin or something) without writing a shell script that will limit the API. For example, I might write a shell script such as: (note that this doesn't really answer all the requirements)
#!/bin/bash
npm install --save $1 && npm install --save-dev @types/$1
但这限制了我,因为也许我想要 --save-dev
两个包或想要在命令中使用一些特殊标志.此外,它创建了对 bash 的依赖,我想避免这种依赖.
But this limits me because maybe I want to --save-dev
both of the packages or want to use some special flags in the command. Also, it creates a dependency on bash which I'd like to avoid.
或者,如果有一种方法可以制作不受这种限制的 shellscript,那也没关系.
Alternatively, if there is a way to make a shellscript that won't be limiting in that way, that would be okay too.
另外,上面的例子实际上并没有检查包是否已经有类型定义(在这种情况下我不想想从@types
下载任何).
Also, the above example doesn't actually check if the package has type definitions already (in which case I don't want to download any from @types
).
推荐答案
以下是一些有助于解决此问题的命令行实用程序
Here are some command line utilities that help with this
为 package.json 中的依赖项安装缺少的 TypeScript 类型
Install missing TypeScript typings for dependencies in your package.json
github - 988 ⭐
npm - 14k/周
# installation npm i -g typesync # usage npx typesync
每次都轻松安装新软件包及其类型.
Easily install new packages and their types, every time.