314
votes

I use TypeScript 2 in my project. I'd like to use some js library, but also typings for that library. I can install types with simple npm install @types/some-library. I'm not sure if I should --save or --save-dev them. It seems to me that even DefinetelyTyped GitHub readme kind of mentions both versions, but never explains them. I would think that @types should be in devDependencies, as types are needed for development and aren't used in runtime, but I saw many times @types in just dependencies. I'm confused.

How should I decide whether @types/* goes into dependencies or devDependencies? Are there actually some more or less official instructions?

3
Are you generating a bundle or is this a package that will be used by others? As I see it you only need to make the distinction between dependencies and devDependencies in the latter case.Valentin
I make some game in js/ts from scratch. I bundle everything with webpack. There's no backend at all atm, but it's possible that I'll wrap it all in Electron to make it standalone some day. I don't think anyone will ever use it as a dependency in their own app, but I guess it could be possible (think of mini games in GTA games; and my game is open source). Still, I want to learn and follow best practices and it's the main reason I make that game. I hope I clarified my use-case well enough. :)kamyl
Yes, it makes sense, just wanted to make sure that my original answer was relevant to your use case. I still think that the distinction between devDependencies and dependencies is irrelevant when building a bundle, it's something that create-react-app enforces as well but ultimately it's up to you to chooseValentin

3 Answers

220
votes

Let's say you're developing a package "A" that have @types/some-module package in devDependencies. For some reason you're exporting the type from @types/some-module

import {SomeType} from 'some-module';
export default class APackageClass {
     constructor(private config: SomeType) {

     }
}

Right now Typescript consumers of package "A" are unable to guess what SomeType is, since devDependencies of package "A" are NOT installed.

In that particular case you NEED to place @types/* package with regular "dependencies". For other cases "devDependencies" are good enough.

75
votes

If you're just generating a bundle there may be no need to make the distinction between dependencies and devDependencies. This feature of npm is generally useful when publishing a package that can be used by others and you don't want to spam them with redundant dependencies.

There may be other use cases where splitting dependencies can be helpful but unless you have an express need for this, then my advice is to just pick either one and place everything there. It's not difficult to split them afterwards if the need should arise.

A well-known example of this practice IRL is create-react-app, by default the un-ejected boilerplate it creates places everything in dependencies, see this thread and this answer

29
votes

In the particular case of deploying a Node.js application to production, one wants to install only the dependencies needed to run the application.

npm install --production or

npm ci --production or

yarn --production

In that case, the types should be in the devDependencies, to keep them from bloating the installation.

Remark: I'm aware this was mentioned in a comment by Brad Wilson to another answer. This point seems worthy to be an answer, though.