In a coding tutorial, an example code snippet demonstrates how to execute a JQuery getJSON() call and then transform the result into a Promise, which is later converted into an Observable.
/// <reference path="../typings/tsd.d.ts" />
import { Component } from '@angular/core';
import { Observable } from 'rxjs/Rx';
@Component({
selector: 'my-app',
template: `
<input id="search" type="text" class="form-control">
`
})
export class AppComponent {
example(searchTerm: string) {
let url: string =
"https://api.spotify.com/v1/search?type=artist&q=" + searchTerm;
let jqueryXhr: JQueryXHR = $.getJSON(url);
let observable: Observable<any> = Observable.fromPromise(jqueryXhr);
}
}
Although this example functions during runtime, the tsc compiler throws an error message:
app/app.component.ts(28,61): error TS2345: Argument of type 'JQueryXHR' is not assignable to parameter of type 'Promise<any>'.
Types of property 'then' are incompatible.
Is there a way to cleanly cast or convert a JQueryXHR object into a Promise or another suitable type that can be transformed into an Observable?