5

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

2 Answers 2

6

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
    
Sign up to request clarification or add additional context in comments.

1 Comment

I thought of running them in postinstall script. But postinstall only runs for npm i and not npm i <package> :( More info.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.