How do I decide whether @types/* goes into `dependencies` or `devDependencies`?

Viewed 100157

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?

4 Answers

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.

(To avoid misunderstandings, the --production option must not be used on machines where the application is built, otherwise the TypeScript compiler will complain.)

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

Other answers made great sense, but I'm gonna add that a peerDep's type declaration package should also be placed in dependencies instead of peerDependencies.

Assume that b is a plugin of a. And c uses a and b.

Why shouldn't @types/a be placed in b's peerDependencies?

If b's package.json is like:

{
  "peerDependencies": {
    "a": "1.5.x"
    "@types/a": "1.4.x"
  }
}

c may use only interfaces defined in @types/a@1.2.x but c is forced to install @types/a@1.4.x.

Furthermore, c may be a regular javascript package rather than typescript package, but c is also forced to install @types/a@1.4.x.

So, the correct package.json of b should be like:

{
  "peerDependencies": {
    "a": "1.5.x"
  },
  "dependencies": {
    "@types/a": "1.4.x"
  }
}
Related