具有严格打字稿的createStore

时间:2018-04-20 13:07:30

标签: typescript redux strictnullchecks

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那样懒惰的事情?

1 个答案:

答案 0 :(得分:-1)

提供的状态值可以不确定。强迫它可以解决您的问题。例如。

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