typescript报错

问题1:

类型“Element[]”的参数不能赋给类型“Element”的参数。
类型“Element[]”缺少类型“Element”中的以下属性: type, props, key

代码:

<Switch>
               {!!routes.length && <Redirect exact strict from="/" to={routes[0].path} />}

               {routes.flatMap((r: MenuItem) => {
                 const routes = [<Route key={r.path} exact path={r.path} component={r.component} />];
                 if (r.children) {
                   const childRoutes = r.children?.map((sub: MenuItem) => {
                     return <Route key={sub.path} exact path={sub.path} component={sub.component} />
                   })
                   routes.push(childRoutes) // 报错如图
                 }
                 return routes;
                 })}

               {/* <Route component={NotFound}/> */}
               <Redirect from="*" to={{ pathname: '/404' }} />
             </Switch>

image.png

问题2:

// 这是我的store代码
import { Dispatch, Action } from 'redux'

export interface AuthAction extends Action {
  data: UserInfo
}
const reducer =  (state = initialState, action: AuthAction) => {
  switch(action.type){
    case types.AUTHORIZE:
        return { ...state, authorizeStatus: false }
    case types.AUTHORIZE_SUCCESS:
        return { ...state, userInfo: action.data, authorizeStatus: true }
    case types.AUTHORIZE_FAILURE:
        return { ...state, authorizeStatus: false}
    default:
        return {...state}
  }
}
export default reducer;


export const authorize =  (cfg: ConfigInfo) => async (dispatch: Dispatch<AuthAction>): Promise<void> => { 
  try {
    dispatch({type: types.AUTHORIZE, data: {}});

    const res = await authService.authorize(cfg);
    dispatch({
      type: types.AUTHORIZE_SUCCESS,
      data: res.data,
    });
    return res.data;

  } catch (err) {
    dispatch({type: types.AUTHORIZE_FAILURE,data: {} });
    throw err;
  }
}

我在store.dispatch的时候报了这么个错误,大概是type是必须的但是我却没有,我是流程写的有问题么?
image.png

阅读 4.3k
1 个回答

你的routes類型是Element,而childRoutesElement[]。你應該用concat而不是push

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进