I'm currently reaching out for help as typescript gave me the following error that I cannot just make sense of it when trying to write a useReducer.
TS2769: No overload matches this call. Overload 1 of 5, '(reducer: ReducerWithoutAction, initializerArg: any, initializer?: undefined): [any, DispatchWithoutAction]', gave the following error. Argument of type '(state: AuthState, action: AuthAction) => Error | { wallet: string | null; profile: { [key: string]: Object[]; } | null; account: { [key: string]: Object[]; } | null; error: { [key: string]: Object[]; } | null; }' is not assignable to parameter of type 'ReducerWithoutAction'. Overload 2 of 5, '(reducer: (state: AuthState, action: AuthAction) => Error | { wallet: string | null; profile: { [key: string]: Object[]; } | null; account: { [key: string]: Object[]; } | null; error: { ...; } | null; }, initialState: never, initializer?: undefined): [...]', gave the following error. Argument of type 'AuthState' is not assignable to parameter of type 'never'.
import React from 'react'
type AuthState = {
wallet: string | null,
profile: { [key: string]: Object[] } | null,
account: { [key: string]: Object[] } | null,
error: { [key: string]: Object[] } | null,
}
type AuthAction = {
type: 'SET_WALLET' | 'SET_ACCOUNT' | 'SET_ERROR' | 'LOGOUT',
payload: AuthState
}
const initialState: AuthState = {
wallet: null,
profile: null,
account: null,
error: null
}
const authReducer = (state: AuthState, action: AuthAction) => {
switch (action.type) {
case 'SET_WALLET':
return {...state, wallet: action.payload.wallet}
case 'SET_ACCOUNT':
return {...state, profile: action.payload.profile, account: action.payload.account}
case 'SET_ERROR':
return {...state, error: action.payload.error}
case 'LOGOUT':
return {...state, wallet: action.payload.wallet}
default:
return new Error(`Unhandled action type ${action.type}`)
}
}
const AuthContext = React.createContext(initialState)
type Props = {
children: JSX.Element
}
const AuthProvider = ({children}: Props) => {
const [state, dispatch] = React.useReducer(authReducer, initialState)
const value = {state, dispatch}
return (
<AuthContext.Provider value={value}>
{children}
</AuthContext.Provider>
)
}
export {AuthContext, AuthProvider}
If only someone could help me understand whys that is and what I am doing wrong.