Specification observed in TypeScript with Angular2

I am trying to create a service that returns an Observable that my components can subscribe to. But I get the following error:

Property 'subscribe' does not exist on type 'Observable'. 

I am currently running build alpha.44, and below you will find code that reproduces the problem.

 import {Http} from 'angular2/http'; import {Observable} from 'angular2/core'; export class Backend { http: Http; constructor(http: Http) { this.http = http; this.getTeams().subscribe(); } public getTeams(): Observable { return this.http.get('/api/teams') .map(JSON.parse); } } 

Changing the code to return "any" type seems to work, but it eliminates some of the benefits of using TypeScript. Is there a good way to use strict types for Observables in current Angular2 builds?

+6
source share
2 answers

Which returns from http.get Observable from rxjs , not angular2 Observable . As a workaround, you can import the rxjs Observable from "@ reactivex / rxjs / dist / cjs / Observable" (see this planner ).

 import Observable from '@reactivex/rxjs/dist/cjs/Observable'; class Backend { // ... getTeams() : Observable { return this.http.get('api/teams.json') .map(res => res.json()); } } 

But IMHO, modifying the code to return "any" is the best solution at the moment. They are doing this in the angular2 http module at the moment.

+6
source

use this to cut ngc errors over methods that return observables

 yourMethod(...arguments): Observable<any> {} 
0
source

All Articles