Install type definitions automatically when I install an npm package

一笑奈何 提交于 2021-02-09 11:47:45

问题


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

  1. Check if it has type definitions inside it
  2. If it does not, try to install @types/PACKAGE with the --save-dev flag

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

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.

Alternatively, if there is a way to make a shellscript that won't be limiting in that way, that would be okay too.

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).


回答1:


I ended up writing a CLI utility that does exactly this. Thin layer on top of your package manager of choice.

Check it out at https://github.com/xavdid/typed-install




回答2:


Here are some command line utilities that help with this

TypeSync

Install missing TypeScript typings for dependencies in your package.json

  • github - 988 ⭐

  • npm - 14k/wk

    # installation
    npm i -g typesync
    # usage
    npx typesync
    

Typed-Install

Easily install new packages and their types, every time.

  • github - 87 ⭐

  • npm - 1k/wk

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

TS-Typie

A small utility for installing TypeScript definition files using npm

  • github - 25⭐

  • npm - 0.3k/wk

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


来源:https://stackoverflow.com/questions/48857430/install-type-definitions-automatically-when-i-install-an-npm-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!