I have been able to build, link and import my library into my main application, however my library provides a service that returns RxJs Observables and I am having an issue where the return type is of the RxJs observable found in the library node_modules, instead of the main application node_modules:
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)
Here is my Angular Library's Workspace's 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"
}
}
and the Library project itself:
{
"name": "state-management",
"version": "0.0.1",
"peerDependencies": {
"@angular/common": "^7.0.0",
"@angular/core": "^7.0.0",
"rxjs": "^6.5"
}
}
the library's workspace's 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/*"
]
}
}
}
my application's 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
}
}
As I understand it peer dependencies should be in tsconfig path - I assume in the library but that didnt work for me so I tried both as you can see above.
This is a problem because this causes the error:
getBusinessClaims(): Observable<IBusinessClaims> {
return this.stateService.get<IBusinessClaims>(ClaimType.Business);
}
The error sounds like the import should be coming from a different location, but in face it was because of a version mixmatch. I imagine it would be a good idea to lock the RxJs package on the minor instead of major in order to avoid this.