網站搜尋

angular - 找不到 'ramda' 的型別定義檔


我有一個使用 Ionic 2 和 Angular 2 的專案。

我在我的程式碼中使用 RamdaJS,這些命令可以完美運作:

  • ionic serve
  • ionic cordova build android
  • ionic cordova run android

但是當我嘗試執行這個命令時:ionic cordova build android --prod --release,我收到了關於 Ramda 和 Type 的錯誤。

查看我部分的 package.json:

"dependencies": {
    "@angular/common": "2.4.8",
    "@angular/compiler": "2.4.8",
    "@angular/compiler-cli": "2.4.8",
    "@angular/core": "2.4.8",
    "@angular/flex-layout": "^2.0.0-rc.1",
    "@angular/forms": "2.4.8",
    "@angular/http": "2.4.8",
    "@angular/material": "2.0.0-beta.2",
    "@angular/platform-browser": "2.4.8",
    "@angular/platform-browser-dynamic": "2.4.8",
    "@angular/platform-server": "2.4.8",
    "@angular/router": "3.4.8",
    "@ionic-native/camera": "3.7.0",
    "@ionic-native/core": "3.7.0",
    "@ionic-native/network": "3.7.0",
    "@ionic-native/splash-screen": "3.7.0",
    "@ionic-native/status-bar": "3.7.0",
    "@ionic/storage": "2.0.0",
    "cordova-plugin-device": "^2.0.1",
    "cordova-plugin-ionic-keyboard": "^2.0.5",
    "cordova-plugin-ionic-webview": "^1.1.16",
    "cordova-plugin-splashscreen": "^5.0.2",
    "cordova-plugin-whitelist": "^1.3.3",
    "hammerjs": "2.0.8",
    "ionic-angular": "2.3.0",
    "ionicons": "3.0.0",
    "material-design-icons": "3.0.1",
    "moment": "2.18.1",
    "moment-duration-format": "1.3.0",
    "ng2-translate": "5.0.0",
    "ng2-webstorage": "1.5.1",
    "ngx-pipes": "1.5.7",
    "ramda": "0.23.0",
    "rxjs": "5.0.1",
    "sw-toolbox": "3.4.0",
    "zone.js": "0.7.2",
    "cordova-android": "~7.0.0"
  },
  "devDependencies": {
    "@ionic/app-scripts": "1.2.2",
    "@types/ramda": "github:types/npm-ramda",
    "@types/moment-duration-format": "1.3.1",
    "typescript": "2.2.1"
  }

tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths" : {
       "ramda": [
         "location-of-types/npm-ramda-package/index"
       ]
     },
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "dom",
      "es2015"
    ],
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5"
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}

嘗試為生產環境構建時出錯:

[12:26:31]  typescript error
            Cannot find type definition file for 'ramda'.

Error: Failed to transpile TypeScript

答案#1

按照 DefinitlyTyped 的建議,透過 @types/packageName 安裝類型。

對於 NPM 執行:

npm install @types/ramda --save-dev

若使用 YARN:

yarn add @types/ramda --dev

TypeScript 預設會在你的 node_modules 內尋找名為 @types 的資料夾來找到型別定義:

預設情況下,所有可見的 “@types” 套件都會包含在您的編譯中。任何包含資料夾中的 node_modules/@types 套件都被視為可見;具體來說,這意味著 ./node_modules/@types/、../node_modules/@types/、../../node_modules/@types/ 以及其他以此類推的套件。

如果指定了 typeRoots,則只會包含 typeRoots 下的套件。

tsconfig.json 文件說明

答案#2

請遵循此處的安裝說明:https://github.com/types/npm-ramda

using npm
npm install --save-dev types/npm-ramda#dist

如果不使用 npm/yarn,您可能需要將這些型別加入到 tsconfig.json 的路徑中:

完整套裝包含:

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths" : {
       "ramda": [
         "location-of-types/npm-ramda-package/index"
       ]
     }
  }
}