5
import { createStore } from "redux";
import * as storage from "redux-storage";

const reducer = storage.reducer((state, action) => ({}));

const store = createStore(reducer, {});

使用strict启用了打字稿的上述代码时,我收到以下警告:

[ts] Argument of type 'Reducer<{}>' is not assignable to parameter of type 'Reducer<{}, AnyAction>'.
       Types of parameters 'state' and 'state' are incompatible.
         Type '{} | undefined' is not assignable to type '{}'.
           Type 'undefined' is not assignable to type '{}'.

我了解 TS 严格模式是什么,但我不明白为什么它将我的状态解释为可能undefined

我可能做错了什么?或者我怎么能在不做一些懒惰的事情的情况下处理错误as any

4

1 回答 1

-1

提供的状态值可以是未定义的。强制它应该可以解决您的问题。例如

const reducer = storage.reducer((state = {}, action) => ({}));
于 2018-06-28T23:56:17.370 回答