安装 npm 包时自动安装类型定义 [英] Install type definitions automatically when I install an npm package

查看:67
本文介绍了安装 npm 包时自动安装类型定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法以这样的方式配置 npm,这样每当我安装一个包时,它就会:

Is there a way to configure npm in such a way so that whenever I install a package, it will:

  1. 检查里面是否有类型定义
  2. 如果没有,请尝试使用 --save-dev 标志安装 @types/PACKAGE
  1. Check if it has type definitions inside it
  2. 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.

    • github - 87 ⭐

      npm - 1k/周

      # installation
      npm i -g typed-install
      # usage
      typedi lodash
      

    • 一个使用 npm 安装 TypeScript 定义文件的小工具

      A small utility for installing TypeScript definition files using npm

      • github - 25⭐

        npm - 0.3k/周

        # installation
        npm i -g ts-typie
        # usage
        npx ts-typie
        

      • 这篇关于安装 npm 包时自动安装类型定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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