0

我已经能够构建、链接并将我的库导入到我的主应用程序中,但是我的库提供了一个返回 RxJs Observables 的服务,并且我遇到了一个问题,即返回类型是在库 node_modules 中找到的 RxJs observable,而不是主要应用程序节点模块:

Type 'import("D:/KeySystems-Angular-Packages/node_modules/rxjs/internal/Observable").Observable<boolean>' is not assignable to type 'import("d:/KeySystems-WebCustomerPortal/node_modules/rxjs/internal/Observable").Observable<boolean>'.
  Types of property 'source' are incompatible.
    Type 'import("D:/KeySystems-Angular-Packages/node_modules/rxjs/internal/Observable").Observable<any>' is not assignable to type 'import("d:/KeySystems-WebCustomerPortal/node_modules/rxjs/internal/Observable").Observable<any>'.ts(2322)

这是我的 Angular 库的 Workspace 的 package.json:

{
  "name": "key-systems-core-workspace",
  "version": "0.0.0",
  "scripts": {},
  "dependencies": {
    "@angular/animations": "~7.2.0",
    "@angular/common": "~7.2.0",
    "@angular/compiler": "~7.2.0",
    "@angular/core": "~7.2.0",
    "@angular/forms": "~7.2.0",
    "@angular/platform-browser": "~7.2.0",
    "@angular/platform-browser-dynamic": "~7.2.0",
    "@angular/router": "~7.2.0",
    "@types/cpx": "^1.5.0",
    "@types/npm": "^2.0.31",
    "core-js": "^2.5.4",
    "cpx": "^1.5.0",
    "npm": "^6.12.1",
    "rxjs": "~6.3.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.13.0",
    "@angular-devkit/build-ng-packagr": "~0.13.0",
    "@angular/cli": "~7.3.5",
    "@angular/compiler-cli": "~7.2.0",
    "@angular/language-service": "~7.2.0",
    "@types/node": "~8.9.4",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "ng-packagr": "^4.2.0",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tsickle": ">=0.34.0",
    "tslib": "^1.9.0",
    "tslint": "~5.11.0",
    "typescript": "~3.2.2"
  }
}

和图书馆项目本身:

{
  "name": "state-management",
  "version": "0.0.1",
  "peerDependencies": {
    "@angular/common": "^7.0.0",
    "@angular/core": "^7.0.0",
    "rxjs": "^6.5"
  }
}

库的工作区的 tsconfig:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": true,
    "declarationMap": true,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ],
    "paths": {
      "state-management": [
        "dist/state-management"
      ],
      "state-management/*": [
        "dist/state-management/*"
      ],
      "rxjs": [
        "./node_modules/rxjs"
      ],
      "rxjs/*": [
        "./node_modules/rxjs/*"
      ]
    }
  }
}

我的应用程序的 tsconfig:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "src",
    "paths": {
      "@shared/*": ["app/shared/*"],
      "rxjs": [
        "node_modules/rxjs"
      ],
      "rxjs/*": [
        "node_modules/rxjs/*"
      ]
    },
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types",
      "src/global-types/typings.d.ts"
    ],
    "lib": [
      "es2017",
      "dom"
    ],
    "resolveJsonModule": true, // allow us to directly import json like modules
    "esModuleInterop": true, // quiets error about default exports when importing json,
    "downlevelIteration": true // supports downleveling iteration to es5 such as Sets
  }
}

据我了解,对等依赖项应该在 tsconfig 路径中​​ - 我假设在库中,但这对我不起作用,所以我尝试了两者,如您在上面看到的。

这是一个问题,因为这会导致错误:

getBusinessClaims(): Observable<IBusinessClaims> {
    return this.stateService.get<IBusinessClaims>(ClaimType.Business);
}
4

1 回答 1

1

该错误听起来像是导入应该来自不同的位置,但实际上这是因为版本混合匹配。我想最好将 RxJs 包锁定在次要而不是主要上以避免这种情况。

于 2020-01-21T15:13:14.977 回答