更新到 TypeScript 2.4.1 后,编译使用可从 @ngrx/effects
(版本 2.0.3)观察到的 Actions
的项目会抛出以下错误:
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 2.4.1 更严格地执行Observable
中lift
方法的通用签名和Actions
observable 的 实现lift
未通过检查。
可以使用 --noStrictGenericChecks
命令行标志或 noStrictGenericChecks
编译器选项(作为 tsconfig.json
中的参数)禁用检查的 “compilerOptions”
对象)。
禁用检查的替代方法是使用 TypeScript 的接口(interface)扩充来指定正确的签名:
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/effects
的发布得到解决 2.0.4 .
关于typescript - 使用 TypeScript 2.4.1 在 @ngrx/effects 中观察到的操作出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44938803/