import { Users } from "config/userList" import { useReducer } from 'react' type User = { Id: number, Image: string, Name: string, DoClap: number, ReciveClap: number, } const initialState: User = {...Users[0]}; enum UserActionType { SET = "SET" } type UserAction = { type: UserActionType; res?: User; } const set = (res: User) => ({ type: UserActionType.SET, res }) const reducer = (state, action: UserAction) => { switch (action.type) { case UserActionType.SET: return [action.res] default: state; } } export const getUser = () => { const [state, dispatch] = useReducer(reducer, initialState); //ここでエラーは発生して const handleSelect = e => { dispatch(set(e.target.value)); } return [state, handleSelect]; }
下記のようなエラーが発生します。
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: any, action: UserAction) => (User | undefined)[] | undefined' is not assignable to parameter of type 'ReducerWithoutAction<any>'. Overload 2 of 5, '(reducer: (state: any, action: UserAction) => (User | undefined)[] | undefined, initialState: (User | undefined)[] | undefined, initializer?: undefined): [...]', gave the following error. Argument of type 'User' is not assignable to parameter of type '(User | undefined)[]'.
エラーの内容で調べても全く歯が立たない状態です.
もし可能であれば、ご教授お願いします。
state の型は User ですか? それとも User[] ですか?
また、UserActionType は enum よりも string literal がいいと思います。
https://www.kabuku.co.jp/developers/good-bye-typescript-enum
回答1件
あなたの回答
tips
プレビュー