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

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