I've created the following component:
import React, {useReducer} from 'react'
type ACTION_TYPE = | {type: 'SET_USER', userId: number} | {type: 'DELETE_USER', userId: number, deleteMessages: boolean}
interface User {
id : number;
name? : string;
surname?: string;
hasDiscount?: boolean;
}
const myReducer = (state : typeof initialState, action : ACTION_TYPE) => {
switch (action.type) {
case 'DELETE_USER':
return {users: [...state.users, {id: action.userId}]}
default:
return state;
}
}
const initialState = {
users : []
}
const TestingComponent : React.FC = () => {
const [state, dispatch] = useReducer(myReducer,initialState)
return (
<div></div>
)
}
export default TestingComponent
I'm getting an error on my useReducer
- No overload matches this call. Overload 1 of 5, '(reducer: ReducerWithoutAction<any>, initializerArg: any, initializer?: undefined): [any, DispatchWithoutAction]', gave the following error. Argument of type '(state: typeof initialState, action: ACTION_TYPE) => { users: { id: number; }[]; }' is not assignable to parameter of type 'ReducerWithoutAction<any>'. Overload 2 of 5, '(reducer: (state: { users: never[]; }, action: ACTION_TYPE) => { users: { id: number; }[]; }, initialState: never, initializer?: undefined): [never, Dispatch<ACTION_TYPE>]', gave the following error. Argument of type '{ users: never[]; }' is not assignable to parameter of type 'never'.ts(2769)
I'm not sure how to interpret this though - I've also tried creating the following interface
interface USER_STATE {
users: [User]
}
and using it in myReducer
to define the type of state
- but this also doesn't work.