我刚刚从 typescript 2.4 升级到 3.2,现在使用未知类型,类型系统更严格,这会触发一些我以前没有的错误。
所以我有一个效果,可以从商店中检索一些可能为空的数据,我想在调度我的下一个操作之前通过使用过滤器来确保它不为空。
@Effect() saveSuccess$: Observable<Action> = this.actions$.pipe(
ofType(actions.SAVE_SUCCESS),
switchMapTo(this.store.pipe(select(selectors.getId))),
filter((id: number | null) => id !== null),
map((id: number) => new actions.GetData({ Id }))
);
Run Code Online (Sandbox Code Playgroud)
过滤器现在是红色的说:
Argument of type 'MonoTypeOperatorFunction<number | null>' is not assignable to parameter of type 'OperatorFunction<number | null, number>'.
Type 'Observable<number | null>' is not assignable to type 'Observable<number>'.
Type 'number | null' is not assignable to type 'number'.
Type 'null' is not assignable to type 'number'.ts(2345)
Run Code Online (Sandbox Code Playgroud)
我可以使用任何类型绕过错误,但我觉得我不应该这样做。如果我更改地图以接受number | null它,但它没有任何意义,因为它正是过滤器的工作。