[英]RXJS Observable enum type, Type 'Observable<Type>' is not assignable to type 'Type'
I am trying to observe the latest event that happens in a stream. 我正在尝试观察流中发生的最新事件。 Each event can be one of two types, represented by an enum. 每个事件可以是由枚举表示的两种类型之一。 However I am getting a compilation error in the following example: 但是在以下示例中出现编译错误:
import { BehaviorSubject } from 'rxjs/Rx';
export enum FilterAction {
RESET, UPDATE
}
export class FilterSomething {
private _filterEvent: BehaviorSubject<FilterAction> = new BehaviorSubject(FilterAction.RESET);
get filterEvent() {
return this._filterEvent.asObservable(); // <-- behave.ts(14,12): error TS2322: Type 'Observable<FilterAction>' is not assignable to type 'FilterAction'
}
set filterEvent(action: FilterAction){
this._filterEvent.next(action);
}
}
Compilation error: behave.ts(14,12): error TS2322: Type 'Observable<FilterAction>' is not assignable to type 'FilterAction'
编译错误: behave.ts(14,12): error TS2322: Type 'Observable<FilterAction>' is not assignable to type 'FilterAction'
However when I use the generic any
it compiles (and works). 然而,当我使用通用的any
它编译(和作品)。
set filterEvent(filterAction: any) {
this._filterEvent.next(filterAction);
}
I suspect it's something to do with the enum values, as opposed to instances of FilterAction
. 我怀疑这与枚举值有关,而不是FilterAction
实例。
The problem is return type of your getter
. 问题是您的getter
返回类型。 Getter
s and Setter
s should take and return same type of parameter, which is FilterAction
in your case. Getter
和Setter
应当采用并返回相同类型的参数,在您的情况下为FilterAction
。 But your getter
returns Observable<FilterAction>
. 但是您的getter
返回Observable<FilterAction>
。 Just do not use getter
, change the name of the function. 只是不要使用getter
,更改函数的名称。 Then language service stop showing error. 然后语言服务停止显示错误。 In language specification: 在语言规范中:
If both accessors include type annotations, the specified types must be identical. 如果两个访问器都包含类型注释,则指定的类型必须相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.