【问题标题】:Error with Actions observable in @ngrx/effects using TypeScript 2.4.1使用 TypeScript 2.4.1 在 @ngrx/effects 中可观察到的操作出错
【发布时间】:2017-07-06 02:21:54
【问题描述】:

更新到 TypeScript 2.4.1 后,编译使用来自 @ngrx/effects(版本 2.0.3)的 Actions observable 的项目会看到抛出以下错误:

Error TS2684: The 'this' context of type 'Actions' is not assignable to method's 'this' of type 'Observable<any>'.
Error TS7006: Parameter 'action' implicitly has an 'any' type.
Error TS2345: Argument of type 'Actions' is not assignable to parameter of type 'Observable<Action>'.
  Types of property 'lift' are incompatible.
    Type '(operator: Operator<any, Action>) => Observable<Action>' is not assignable to type '<R>(operator: Operator<Action, R>) => Observable<R>'.
      Types of parameters 'operator' and 'operator' are incompatible.
        Type 'Operator<Action, R>' is not assignable to type 'Operator<any, Action>'.
          Type 'R' is not assignable to type 'Action'.

如何解决?

【问题讨论】:

    标签: typescript ngrx


    【解决方案1】:

    TypeScript 2.4.1 更严格地强制执行 Observablelift 方法的通用签名,而 Actions observable 的 lift 实现未通过检查。

    可以使用--noStrictGenericChecks 命令行标志或noStrictGenericChecks 编译器选项(作为tsconfig.json"compilerOptions" 对象中的参数)禁用检查。

    禁用检查的另一种方法是使用 TypeScript 的接口扩充来指定正确的签名:

    import { Action } from "@ngrx/store";
    import { Observable } from "rxjs/Observable";
    import { Operator } from "rxjs/Operator";
    
    declare module "@ngrx/effects/src/actions" {
        interface Actions {
            lift<R>(operator: Operator<Action, R>): Observable<R>;
        }
    }
    

    请注意,必须使用模块/文件的完整路径;仅将 @ngrx/effects 指定为模块名称是不够的。


    @ngrx/effects2.0.4 的发布已解决此问题。

    【讨论】:

    • 我遇到了与Subject 类相关的类似错误。解决方法是升级 rxjs 包(在我的情况下升级到 5.5.7)。
    猜你喜欢
    • 2019-11-04
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 2018-12-02
    相关资源
    最近更新 更多