【问题标题】:What's the equivalence of typings globalDevDependencies for npm @types packages?npm @types 包的类型 globalDevDependencies 的等价性是什么?
【发布时间】:2017-04-14 14:37:27
【问题描述】:

我想将一个 tsc@1.8 项目升级到 tsc@2,并在此过程中从我的工具链中删除 typings

对于常见的依赖来说这不是问题,因为这些依赖来自我的typings.json:

"dependencies": {
  "bluebird": "registry:npm/bluebird#3.3.4+20160515010139",
  "lodash": "registry:npm/lodash#4.0.0+20160416211519",
  "mime": "registry:npm/mime#1.3.0+20160423043021"
}

我可以通过以下方式轻松安装

npm i @types/bluebird @types/lodass @types/mime

但我的typings.json 中还有一些globalDevDependencies 用于我的测试设置:

"globalDevDependencies": {
  "mocha": "registry:dt/mocha#2.2.5+20160317120654"
}

我的第一次尝试是:

npm install @types/mocha --save-dev

但现在tsc 抱怨它不知道mocha 函数itdescribe

tests/unit/HelloServiceTest.ts(4,1): error TS2304: Cannot find name 'describe'.
tests/unit/HelloServiceTest.ts(5,5): error TS2304: Cannot find name 'it'.
tests/unit/HelloServiceTest.ts(10,5): error TS2304: Cannot find name 'it'.

作为一个远景,我错误地认为在全球范围内安装这些可能会解决问题:

npm i @types/mocha  -g

我还偶然发现了this issue,其中的解决方案是不排除tsconfig.json 中的类型文件夹:

"exclude": [
    "node_modules",
    "!node_modules/@types"
]

但它也对我不起作用,抛出同样的错误。

最后,当我只想使用npm@types/*包而不是@987654343时,我不知道如何达到typings'globalDevDependenciesglobalDependencies的相同效果@。

【问题讨论】:

    标签: node.js typescript types npm typescript-typings


    【解决方案1】:

    This thread 为我指明了正确的方向,因为我必须将类型添加到 tsconfig.json

    {
      "compilerOptions": {
        "target": "ES6",
        "types": ["node", "mocha", "chai"],
        ...
    }
    

    The types option also have a verbose documentation.

    【讨论】:

    • 谢谢!这对我有用。我对文档的解释是,默认情况下应该包含这些类型,所以如果没有它就无法工作,这有点令人困惑。 “默认情况下,所有可见的“@types”包都包含在您的编译中。”
    猜你喜欢
    • 2016-09-29
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2017-06-01
    • 2014-06-24
    • 1970-01-01
    • 2017-03-01
    相关资源
    最近更新 更多