Eu tenho store.js
arquivo
import { createStore, combineReducers } from "redux";
import reducerADD from "../reducer/reducerADD"
export const store = createStore(combineReducers({ reducerADD}));
Quando altero o formato, store.tsx
recebo um erro:
No overload matches this call.
Overload 1 of 2, '(reducers: ReducersMapObject<{ reducerADD: { lastName: string; firstName: string; password: string; email: string; }[]; }, any>): Reducer<{ reducerADD: { lastName: string; firstName: string; password: string; email: string; }[]; }, AnyAction>', gave the following error.
Type '(state: never[] | undefined, action: action) => { lastName: string; firstName: string; password: string; email: string; }[]' is not assignable to type 'Reducer<{ lastName: string; firstName: string; password: string; email: string; }[], any>'.
Types of parameters 'state' and 'state' are incompatible.
Type '{ lastName: string; firstName: string; password: string; email: string; }[] | undefined' is not assignable to type 'never[] | undefined'.
Type '{ lastName: string; firstName: string; password: string; email: string; }[]' is not assignable to type 'never[]'.
Type '{ lastName: string; firstName: string; password: string; email: string; }' is not assignable to type 'never'.
Overload 2 of 2, '(reducers: ReducersMapObject<{ reducerADD: { lastName: string; firstName: string; password: string; email: string; }[]; }, AnyAction>): Reducer<{ reducerADD: { lastName: string; firstName: string; password: string; email: string; }[]; }, AnyAction>', gave the following error.
Type '(state: never[] | undefined, action: action) => { lastName: string; firstName: string; password: string; email: string; }[]' is not assignable to type 'Reducer<{ lastName: string; firstName: string; password: string; email: string; }[], AnyAction>'.
Types of parameters 'state' and 'state' are incompatible.
Type '{ lastName: string; firstName: string; password: string; email: string; }[] | undefined' is not assignable to type 'never[] | undefined'.
Type '{ lastName: string; firstName: string; password: string; email: string; }[]' is not assignable to type 'never[]'.
Qual é a razão para isto?
typescript
react-redux
Jesus
fonte
fonte
reducerADD
?const reducerADD = (state = [], action:action) =>{ switch (action.type) { case "ADD": return [...state, ...[action.add]]; default: return state; } }
Respostas:
O estado precisa ser um pouco mais digitado. Está tentando converter seu estado inicial de um tipo
any
para outronever
.Ao criar o estado padrão para os dados, você deve definir uma interface para o seu estado, aqui está um exemplo para uma matriz de lista de tarefas:
fonte