redux - createStore con TypeScript estricto
import { createStore } from "redux";
import * as storage from "redux-storage";
const reducer = storage.reducer((state, action) => ({}));
const store = createStore(reducer, {});
Al usar el código anterior con strict habilitado para TypeScript, recibo la siguiente advertencia:
[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 '{}'.
Entiendo qué es el modo estricto de TS, pero no entiendo por qué interpreta mi estado como potencialmente undefined
¿Qué podría estar haciendo mal? ¿O cómo podría manejar el error sin hacer algo perezoso como as any?
Respuesta #1
El valor del estado proporcionado puede ser indefinido. Forzarlo debería solucionar tu problema. Por ejemplo.
const reducer = storage.reducer((state = {}, action) => ({}));
