简体   繁体   English

RXJS可观察的枚举类型,类型为“可观察” <Type> &#39;不可分配给&#39;Type&#39;类型

[英]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. GetterSetter应当采用并返回相同类型的参数,在您的情况下为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. 如果两个访问器都包含类型注释,则指定的类型必须相同。

You can check this out. 您可以检查这个出来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 输入&#39;可观察的<Object> &#39; 不可分配给类型 &#39;Observable<IUser[]> &#39; - Type 'Observable<Object>' is not assignable to type 'Observable<IUser[]>' 不可分配给“可观察”类型 <task> &#39;。角4 - is not assignable to type 'Observable<task>'.Angular 4 无法将“订阅”类型分配给“可观察”类型 <Exercise[]> “ - Type 'Subscription' is not assignable to type 'Observable<Exercise[]>' 可观察的 <void | AuthError> &#39;不可分配给&#39;Observable类型 <Action> - Observable<void | AuthError>' is not assignable to type 'Observable<Action> 输入&#39;可观察的<HttpEvent<T> &gt;&#39; 不可分配到类型 &#39;Observable<T> &#39; - Type 'Observable<HttpEvent<T>>' is not assignable to type 'Observable<T>' 类型“可观察” <boolean | “”> &#39;不可分配给&#39;Observable类型 <boolean> &#39;TS2322 - Type 'Observable<boolean | “”>' is not assignable to type 'Observable<boolean>' TS2322 输入&#39;可观察的<Response> &#39; 不可分配给类型 &#39;Observable <HttpResponse<Blob> &gt;&#39; - Type 'Observable<Response> ' is not assignable to type 'Observable<HttpResponse<Blob>>' RxJs管道和可调运算符`map`:'this'类型的'this'上下文不能赋值给'Observable <{}>'类型的方法'this' - RxJs pipe and lettable operator `map`: 'this' context of type 'void' is not assignable to method's 'this' of type 'Observable<{}>' 类型&#39;日期&#39;不能分配给&#39;Observable&#39;类型 <Date> &#39; - Angular 6+ - Type 'Date' is not assignable to type 'Observable<Date>' - Angular 6+ angular5:类型“true”不可分配给类型“Observable”<boolean> - angular5: Type 'true' is not assignable to type 'Observable<boolean>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM