【问题标题】:React typescript, useReducer TS2769: No overload matches this callReact typescript,useReducer TS2769:没有重载匹配这个调用
【发布时间】:2022-01-28 20:37:09
【问题描述】:

我目前正在寻求帮助,因为 typescript 给了我以下错误,我在尝试编写 useReducer 时无法理解它。

TS2769:没有与此调用匹配的重载。重载 1 of 5, '(reducer: ReducerWithoutAction, initializerArg: any, initializer?: undefined): [any, DispatchWithoutAction]',给出了以下错误。 '(state: AuthState, action: AuthAction) => Error | 类型的参数{钱包:字符串|无效的;配置文件:{ [键:字符串]:对象 []; } | 无效的;帐户:{[键:字符串]:对象[]; } |无效的;错误:{ [键: 字符串]:对象[]; } |无效的; }' 不可分配给类型参数 'ReducerWithoutAction'。重载 2 of 5, '(reducer: (state: AuthState,动作:AuthAction) => 错误 | {钱包:字符串|无效的; 配置文件:{ [键:字符串]:对象 []; } |无效的;帐户:{ [密钥: 字符串]:对象[]; } |无效的;错误: { ...; } |无效的; },初始状态: never, initializer?: undefined): [...]',给出了以下错误。 'AuthState' 类型的参数不能分配给'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}

如果有人可以帮助我理解这是为什么以及我做错了什么。

【问题讨论】:

    标签: reactjs typescript


    【解决方案1】:
    default:
      return new Error(`Unhandled  action type ${action.type}`);
    

    您收到类型错误的原因是因为这一行。如果达到这种情况,那么您将返回一个错误对象,将您的状态替换为该状态而不是 AuthState 对象。

    如果你只是想记录错误,你可以这样做:

    default: 
      console.error(`Unhandled action type ${action.type}`);
      return state;
    

    如果你想让它爆炸,你可以抛出一个错误:

    default:
      throw new Error(`Unhandled  action type ${action.type}`)
    

    或者如果代码是正确的并且您希望将您的状态替换为错误对象,您可以更改reducer的类型以知道错误是可能的:

    const authReducer = (state: AuthState | Error, action: AuthAction) => {
    

    【讨论】:

    • 干杯伙伴,现在一切都说得通了。我希望打字稿错误对此更清楚:D
    • 是的,有时错误可能有点神秘。如果它有助于将来的调试,这就是我所做的事情:我拿了你的代码,然后给函数一个明确的返回类型。即const authReducer = (state: AuthState, action: AuthAction): AuthState =&gt; {。这完全改变了它显示的错误消息,并将错误指示器移至默认情况。有时使类型更明确可以帮助打字稿更好地指出问题。
    • 感谢您的解释。从现在开始,我会在未来这样做。另外,现在我注意到 AuthContext.Provider 中的值显示了一条错误消息TS2739: Type '{ state: AuthState; dispatch: Dispatch&lt;AuthAction&gt;; }' is missing the following properties from type 'AuthState': wallet, profile, account, error index.d.ts(338, 9): The expected type comes from property 'value' which is declared here on type 'IntrinsicAttributes &amp; ProviderProps&lt;AuthState&gt;' 知道那可能是什么吗?
    • const AuthContext = React.createContext(initialState) 基于这一行,typescript 期望您只传递一个 AuthState 对象作为值,但您传递的是一个具有 state 属性和 @987654331 的对象@ 财产。尝试做const AuthContext = React.createContext({ state: initialState, dispatch: () =&gt; {} });
    • TS2322: Type '{ state: AuthState; dispatch: React.Dispatch&lt;AuthAction&gt;; }' is not assignable to type '{ state: AuthState; dispatch: () =&gt; void; }'.   Types of property 'dispatch' are incompatible.     Type 'Dispatch&lt;AuthAction&gt;' is not assignable to type '() =&gt; void'. index.d.ts(338, 9): The expected type comes from property 'value' which is declared here on type 'IntrinsicAttributes &amp; ProviderProps&lt;{ state: AuthState; dispatch: () =&gt; void; }&gt;' 现在它给了我这个
    猜你喜欢
    • 2020-08-22
    • 2020-06-14
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    相关资源
    最近更新 更多