user938e3ef455
user938e3ef455

Reputation: 349

Angular 4 to 5; Type 'Observable<{}>' is not assignable to type error

I upgraded from Angular 4 to 5 and I am getting a type error.

Type 'Observable<{}>' is not assignable to type 'Observable<Device[]>'

I am unsure how to solve this. I did see this post: Type 'Observable<any>' is not assignable to type '[]'. But I don't follow how I can use it to solve my issue.

Here's 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);
    });
  }
}

Here's 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);
        }
      );
    });
  }
}

Here's the package.json:

"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",
    "@angular/router": "^5.0.0",
    "@ng-bootstrap/ng-bootstrap": "^1.0.0-beta.3",
    "angular-ui-bootstrap": "^2.5.0",
    "bootstrap": "^3.3.7",
    "core-js": "^2.5.1",
    "express": "^4.15.4",
    "ng2-file-upload": "^1.2.1",
    "ngx-modialog": "^3.0.4",
    "ngx-pipes": "^1.6.5",
    "rxjs": "^5.5.2",
    "zone.js": "^0.8.14"
  },
  "devDependencies": {
    "@angular/cli": "1.3.2",
    "@angular/compiler-cli": "^5.0.0",
    "@angular/language-service": "^4.2.4",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "^2.0.3",
    "@types/node": "~6.0.60",
    "codelyzer": "~3.1.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "^1.7.1",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.3.2",
    "typescript": "~2.4.2"
  }

Upvotes: 0

Views: 1243

Answers (1)

bryan60
bryan60

Reputation: 29335

typescript 2.4.2+ is a bit more restrictive than earlier versions, you need to be better about typing.

you need to either define getMyDevices() as:

 getMyDevices(): Promise<Device[]> {... fetch the data ...}

or if it's return type is any you could probably get away with:

this.listDeviceService.getMyDevices().then(function (data: Device[]) {
  scope.devices = Observable.of(data);
});

Upvotes: 1

Related Questions