前提・実現したいこと
React, Redux, TypeScriptを使用してアプリ開発しています。
Reduxのreducer内のpayloadの値で怒られるので正しく設定したいです。
バージョン:
react@16.9.0
typescript@3.6.2
redux@4.0.4
発生している問題・エラーメッセージ
TS2339 Property 'result' does not exist on type '{ params: PostParams }'
該当のソースコード
まず、以下がactionです。エラーを指摘されているのはreducerですが、原因やヒントがここにある気もしているので、載せておきます。
TypeScript
1export enum PostActionType { 2 POST_START = 'POST_START', 3 POST_SUCCEED = 'POST_SUCCEED', 4 POST_FAIL = 'POST_FAIL', 5} 6 7interface PostParams { 8 userId: string | null 9} 10 11interface PostResult { 12 type: PostActionType 13 payload: { 14 users: User[] 15 } 16} 17 18export const Post = { 19 start: (params: PostParams) => ({ 20 type: PostActionType.POST_START, 21 payload: { 22 params, 23 }, 24 }), 25 26 succeed: (params: PostParams, result: PostResult) => ({ 27 type: PostActionType.POST_SUCCEED, 28 payload: { 29 params, 30 result, 31 }, 32 }), 33 fail: (params: PostParams, error: AxiosError) => ({ 34 type: PostActionType.POST_FAIL, 35 payload: { 36 params, 37 error, 38 }, 39 error: true, 40 }), 41} 42 43export type PostAction = 44 | ReturnType<typeof Post.start> 45 | ReturnType<typeof Post.succeed> 46 | ReturnType<typeof Post.fail> 47
そして、以下がreducerです。
case PostActionType.POST_SUCCEEDの
return値の
users: action.payload.result.users,
の'result'に赤線が引かれて怒られてしまいます。
TypeScript
1中略 2const postReducer: Reducer<PostState, PostAction> = ( 3 state: PostState = initialState, 4 action: PostAction, 5): PostState => { 6 switch (action.type) { 7 case PostActionType.POST_START: 8 return { 9 ...state, 10 users: [], 11 } 12 case PostActionType.POST_SUCCEED: 13 return { 14 ...state, 15 users: action.payload.result.users, 16 } 17 case PostActionType.POST_FAIL: 18 return { 19 ...state, 20 error: action.payload.error, 21 } 22 default: { 23 return state 24 } 25 } 26} 27 28export default postReducer
試したこと
actionのpayloadを以下のように変えたりしました。
payload: { params, result: result.payload.users, },
あなたの回答
tips
プレビュー