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.
我应该如何决定是否@types/*进入依赖项或devDependencies?有多少官方的指示吗?
其他答案很有意义,但我要补充的是,peerDep的类型声明包也应该放在dependencies中,而不是peerDependencies中。
假设b是a的一个插件,c使用a和b。
为什么不应该把@types/a放在b的peerDependencies中?
如果b是包裹。Json是这样的:
{
"peerDependencies": {
"a": "1.5.x"
"@types/a": "1.4.x"
}
}
C只能使用@types/a@1.2.x中定义的接口,但C被迫安装@types/a@1.4.x。
此外,c可能是一个常规的javascript包,而不是typescript包,但c也被迫安装@types/a@1.4.x。
所以,正确的包装。Json (b)应该像这样:
{
"peerDependencies": {
"a": "1.5.x"
},
"dependencies": {
"@types/a": "1.4.x"
}
}
假设您正在开发一个包“a”,它在devDependencies中有@types/some-module包。出于某种原因,你从@types/some-module导出了类型:
import { SomeType } from 'some-module';
export default class APackageClass {
constructor(private config: SomeType) {
// …
}
}
现在,包“A”的TypeScript使用者无法猜出SomeType是什么,因为包“A”的devDependencies还没有安装。
在这种特殊情况下,您需要将@types/*包与常规依赖项放在一起。对于其他情况,devDependencies已经足够好了。