網站搜尋

node.js - npm @types 套件中 typings globalDevDependencies 的對應是什麼?


我想將一個 [email 項目升級到 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 的函數 it 和 describe。

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

我也碰到過這個問題,解決方法是在 tsconfig.json 中不要排除 types 資料夾:

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

但它對我也不起作用,仍然拋出相同的錯誤。

最後,我不知道當我只想使用 npm 和 @types/* 套件而不是 typings 時,如何達到 typings 的 globalDevDependencies 和 globalDependencies 所有的效果。

答案#1

這個討論串指引了我正確的方向,因為我必須將類型添加到 tsconfig.json 中:

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

types 選項也有詳細的說明文件。