使用TypeScript 2.4.1在@ ngrx/effects中观察动作时出错

car*_*ant 7 typescript ngrx

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

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'.
Run Code Online (Sandbox Code Playgroud)

怎么能被重新取消?

car*_*ant 12

TypeScript 2.4.1更严格地强制执行lift方法的通用签名,Observable并且Actionsobservable的实现lift未通过检查.

可以使用--noStrictGenericChecks命令行标志或noStrictGenericChecks编译器选项(作为tsconfig.json's "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>;
    }
}
Run Code Online (Sandbox Code Playgroud)

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


这已通过@ngrx/effects 2.0.4的发布得到解决.