TypeScript 模块增强 [英] TypeScript module Augmentation
本文介绍了TypeScript 模块增强的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有可观察的扩展.它工作得很好,但现在我已经用 typescript 2.7.2 更新到了 angular 6.
I have extension for observable. It was working perfectly fine but now I've updated to angular 6 with 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;
此代码不起作用
- 'Observable' 仅指一种类型,但在此处用作值.
- Observable"类型不存在subscribe"属性.
https://www.typescriptlang.org/docs/handbook/declaration-合并.html
推荐答案
合并声明时,指定的模块路径必须与实际模块的路径完全匹配.
When merging declarations, the specified module path must exactly match the path to the actual module.
使用 RxJS 版本 6,您将需要更改模块声明,因为内部结构已更改.凭记忆,应该是:
With RxJS version 6, you will need to change your module declaration, as the internal structure has changed. From memory, it should be:
declare module 'rxjs/internal/Observable' {
export interface Observable<T> {
safeSubscribe<T>(this: Observable<T>, component: BaseComponent,
next?: (value: T) => void, error?: (error: T) => void, complete?: () => void): Subscription;
}
}
例如,请参阅其中一个在 rxjs-compat
中修补导入.
For an example, see one of the patching imports in rxjs-compat
.
这篇关于TypeScript 模块增强的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文