Recently, I encountered an issue with my observable extension. Everything was functioning perfectly until I updated to angular 6 and typescript 2.7.2.
import { Observable } from 'rxjs/Observable';
import { BaseComponent } from './base-component';
import { Subscription } from 'rxjs/Subscription';
import { Subscribable } from 'rxjs';
declare module 'rxjs/Observable' {
export interface Observable<T> {
safeSubscribe<T>(this: Observable<T>, component: BaseComponent,
next?: (value: T) => void, error?: (error: T) => void, complete?: () => void): Subscription;
}
}
export function safeSubscribe<T>(this: Observable<T>, component: BaseComponent,
next?: (value: T) => void, error?: (error: T) => void, complete?: () => void): Subscription {
let sub = this.subscribe(next, error, complete);
component.markForSafeDelete(sub);
return sub;
}
Observable.prototype.safeSubscribe = safeSubscribe;
However, the code above is now causing issues:
- 'Observable' only refers to a type, but is being used as a value here.
- Property 'subscribe' does not exist on type 'Observable'.
Visit this link for more information on declaration merging in TypeScript