I recently updated my Angular version from 4 to 5 and encountered a type error.
The type 'Observable<{}>' is not compatible with the type 'Observable<Device[]>'
I am struggling to find a solution for this issue. I came across a similar post here: Type 'Observable<any>' is not assignable to type '[]'. However, I am unable to apply the solution to my problem.
Here's the content of the ts file:
import {Component, OnInit} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import {ListDeviceService} from '../../services/list-device.service';
interface Device {
name: string;
id: string;
}
@Component({
selector: 'app-product',
templateUrl: './product.component.html'
})
export class ProductComponent implements OnInit {
devices: Observable<Device[]>;
constructor(private listDeviceService: ListDeviceService) {
}
ngOnInit() {
const scope = this;
this.listDeviceService.getMyDevices().then(function (data) {
scope.devices = Observable.of(data);
});
}
}
Below is the code for the list-device.service:
import {Injectable} from '@angular/core';
import {AuthenticationService} from '../services/authentication.service';
@Injectable()
export class ListDeviceService {
constructor(private authenticationService: AuthenticationService) {
}
getMyDevices() {
const scope = this;
return new Promise((resolve, reject) => {
const token = this.authenticationService.getToken();
const devicesPr = this.authenticationService.getPartsAPI().listDevices({auth: token});
devicesPr.then(
function (devices) {
console.log('list', devices.body);
resolve(devices.body);
},
function (err) {
reject(err);
}
);
});
}
}
Lastly, here's the package.json file:
"dependencies": {
"@angular/animations": "^5.0.0",
"@angular/common": "^5.0.0",
"@angular/compiler": "^5.0.0",
"@angular/core": "^5.0.0",
"@angular/forms": "^5.0.0",
"@angular/http": "^5.0.0",
"@angular/platform-browser": "^5.0.0",
"@angular/platform-browser-dynamic": "^5.0.0",
"@angular/platform-server": "^5.0.0",
...
},
"devDependencies": {
"@angular/cli": "1.3.2",
"@angular/compiler-cli": "^5.0.0",
"@angular/language-service": "^4.2.4",
...
}