visulalStudioCodeのバージョンが上がってtypescriptのバージョンが2.4.1にアップデートすると
lintエラーが出ました。
- 該当コード
typescript
1export default combineReducers({ 2 mainFeeds, 3 accounts, 4 loginToken 5}); 6
- エラー内容
severity: 'エラー' message: 'Argument of type '{ mainFeeds: (state: Feed | undefined, action: Action) => Feed; accounts: (state: Accounts | unde...' is not assignable to parameter of type 'ReducersMapObject'. Property 'mainFeeds' is incompatible with index signature. Type '(state: Feed | undefined, action: Action) => Feed' is not assignable to type 'Reducer<any>'. Types of parameters 'action' and 'action' are incompatible. Type 'A' is not assignable to type 'Action'. Type 'Redux.Action' is not assignable to type 'Action'. Type 'Action' is not assignable to type '{ type: "INIT"; payload: undefined; }'. Property 'payload' is missing in type 'Action'. Type 'A' is not assignable to type '{ type: "DO_NOTHING"; payload: undefined; }'. Type 'Action' is not assignable to type '{ type: "DO_NOTHING"; payload: undefined; }'. Property 'payload' is missing in type 'Action'.'
上記エラー内容を理解してみると
typescript
1 2declare namespace Redux { 3 interface Action { 4 type: any; 5 } 6 type Reducer<S> = <A extends Action>(state: S, action: A) => S; 7 interface ReducersMapObject { 8 [key: string]: Reducer<any>; 9 } 10 function combineReducers<S>(reducers: ReducersMapObject): Reducer<S>; 11} 12
上記定義にたどり着きました。
一方私の定義しているActionの定義は以下のようになっています。
typescript
1export type Action = 2 { 3 type: 'INIT'; 4 payload: undefined; 5 } | 6 { 7 type: 'FETCH_MAIN_FEEDS'; 8 payload: undefined; 9 } | 10 { 11 type: 'SET_MAIN_FFEDS'; 12 payload: comm.Contentlist; 13 } | 14 { 15 type: 'FETCH_ACCOUNT'; 16 payload: FetchAccountPayload; 17 } | 18// 以降省略 19
つまり、わたしのActionはRedux.Actionを継承した実装になっていないのが問題なのかと思われますが
上記のようにunionによってActionを定義している場合、combineReducersに適合するように最小限の変更で
Actionを再定義するにはどうしたらいいものでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。