typescript 我如何决定@types/* 是进入`dependencies` 还是`devDependencies`?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45176661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:45:45  来源:igfitidea点击:

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

typescriptnpmtypescript-typingspackage.json

提问by kamyl

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 --saveor --save-devthem. 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.

我在我的项目中使用 TypeScript 2。我想使用一些 js 库,但也想使用该库的类型。我可以用 simple 安装类型npm install @types/some-library。我不确定我应该--save还是--save-dev他们。在我看来,即使DefinelyTyped GitHub 自述文件也提到了这两个版本,但从未解释过它们。我认为 @types 应该devDependenciesdependencies. 我糊涂了。

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

我应该如何决定是 @types/* 进入dependencies还是devDependencies?实际上是否有一些或多或少的官方说明?

采纳答案by wookieb

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

假设您正在开发一个包“A”,它在 devDependencies 中有 @types/some-module 包。出于某种原因,您从 @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.

现在包“A”的 Typescript 消费者无法猜测 SomeType 是什么,因为包“A”的 devDependencies 没有安装。

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

在这种特殊情况下,您需要将 @types/* 包与常规“依赖项”放置在一起。对于其他情况,“devDependencies”就足够了。

回答by Valentin

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

如果您只是生成一个包,则可能无需区分dependenciesdevDependenciesnpm当发布一个可以被其他人使用的包并且您不想用冗余依赖项向他们发送垃圾邮件时,此功能通常很有用。

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 threadand this answer

这种做法 IRL 的一个众所周知的例子是create-react-app,默认情况下,它创建的未弹出样板将所有内容放入dependencies,请参阅此线程此答案

回答by Carsten Führmann

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

在将 Node.js 应用程序部署到生产环境的特殊情况下,人们只想安装运行应用程序所需的依赖项。

npm install --productionor

npm install --production或者

npm ci --productionor

npm ci --production或者

yarn --production

yarn --production

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

在这种情况下,类型应该在 中devDependencies,以防止它们膨胀安装。

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.

备注:我知道这是在 Brad Wilson 对另一个答案的评论中提到的。不过,这一点似乎值得作为答案。