简体   繁体   English

使用 Redux 调度操作出现 TS 错误,“类型上不存在属性”

[英]Getting TS error with Redux dispatch action, `Property does not exist on type`

I have the following redux-thunk action:我有以下 redux-thunk 动作:

import { Action } from "redux";
import { ThunkAction as ReduxThunkAction } from "redux-thunk";
import { IState } from "./store";

type TThunkAction = ReduxThunkAction<void, IState, unknown, Action<string>>;

export type TReturnValue = { someString: string };
export const someAction = (): TThunkAction => (dispatch): TReturnValue => ({
  someString: "some value"
});

And I dispatch the action like this:我像这样发送动作:

const { someString } = dispatch(someAction());

which throws the error,引发错误,

const { someString } = dispatch(someAction());
        ^^^^^^^^^^
Property 'someString' does not exist on type 'TThunkAction<void, IState, unknown, Action<string>>'

I've tried casting the result like this,我试过像这样投射结果,

const { someString } = dispatch<TReturnValue>(someAction());

And I've tried redefining TThunkAction , but nothing worked.而且我尝试重新定义TThunkAction ,但没有任何效果。

How can I get Typescript to know what type to expect from the dispatch action?我怎样才能让 Typescript 知道调度操作的期望类型?

优雅的 yalow essnz?

I don't think dispatch() returns anything.我不认为 dispatch() 返回任何东西。 Dispatch is just to send the action to the reducers where you would read the payload of the actions. Dispatch 只是将操作发送到减速器,您将在其中读取操作的有效负载。

If you need a value from your thunk you should fire a new action inside the thunk and handle that it in the reducer.如果您需要 thunk 中的值,您应该在 thunk 中触发一个新操作并在 reducer 中处理它。

To read the value in your react component you should also be using useSelector to read specific values from your state.要读取反应组件中的值,您还应该使用 useSelector 从 state 中读取特定值。

Would be easier to explain if I knew what you where trying to do with the value.如果我知道你试图用价值做什么,会更容易解释。

Take a look at redux with typescript .看看redux 和 typescript
Or consider Redux toolkit for less boilerplate code with Typescript.或者考虑使用 Redux 工具包,使用 Typescript 来减少样板代码。

You can add the fix that @Linda Paiste linked to in the comments manually, I added it and got the correct solution.您可以手动添加@Linda Paiste 在评论中链接到的修复程序,我添加了它并得到了正确的解决方案。

In actions.ts, I fixed the ReduxThunkAction to have the correct value and added the Dispatch type from the patch.在 actions.ts 中,我修复了 ReduxThunkAction 以获得正确的值,并从补丁中添加了 Dispatch 类型。

type ThunkAction<ReturnType> = ReduxThunkAction<
  ReturnType,
  IState,
  unknown,
  Action<string>
>;

export type TReturnValue = { someString: string };
export type TAnotherReturnValue = { anotherString: string };

export const someAction = (): ThunkAction<TReturnValue> => (dispatch) => ({
  someString: "some value"
});
export const anotherAction = (): ThunkAction<TAnotherReturnValue> => (
  dispatch
) => ({
  anotherString: "another value"
});

export interface Dispatch<A extends Action = AnyAction> {
  <TReturnType = any, TState = any, TExtraThunkArg = any>(
    thunkAction: ReduxThunkAction<TReturnType, TState, TExtraThunkArg, A>
  ): TReturnType;
}

In app.tsx, I just force the dispatch type to be the new manually added thunk type.在 app.tsx 中,我只是强制调度类型为新的手动添加的 thunk 类型。

  const dispatch: Dispatch = useDispatch();

See code sandbox:见代码沙箱:

https://codesandbox.io/s/holy-field-vujrq?file=/src/App.tsx https://codesandbox.io/s/holy-field-vujrq?file=/src/App.tsx

EDIT : Answered the comment by making the ThunkAction helper as generic.编辑:通过将 ThunkAction 助手设为通用来回答评论。 The main point about this is to remember that the helper is just a helper and you could also just type each action separately as a ReduxThunkAction , the helper just saves some keystrokes and ensures consistency.关于这一点的要点是要记住 helper 只是一个 helper,您也可以将每个动作单独键入为ReduxThunkAction ,helper 只是保存一些击键并确保一致性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 class 转换为挂钩获取属性 'then' 在类型 '(dispatch: any) => Promise 上不存在<void> '.ts(2339)</void> - converting class to hooks getting Property 'then' does not exist on type '(dispatch: any) => Promise<void>'.ts(2339) 获取 TS 错误,“类型 &#39;TPage&#39; 上不存在属性 &#39;children&#39;” - Getting TS error, "Property 'children' does not exist on type 'TPage'" Redux 操作中的“void”类型不存在属性“数据” - Property 'data' does not exist on type 'void' in Redux action 属性 'current' 在类型 '[boolean, Dispatch 上不存在<setstateaction<boolean> >]'.ts(2339) </setstateaction<boolean> - Property 'current' does not exist on type '[boolean, Dispatch<SetStateAction<boolean>>]'.ts(2339) 错误TS2339:类型“字符串”上不存在属性“默认”。** - Error TS2339: Property 'Default' does not exist on type 'string'.** 错误 TS2339:类型“CustomerTransfert[]”上不存在属性“num” - error TS2339: Property 'num' does not exist on type 'CustomerTransfert[]' 错误TS2339:类型“ HTMLElement”上不存在属性“ imageID” - error TS2339: Property 'imageID' does not exist on type 'HTMLElement' 错误TS:2339类型{}上没有属性“ Project” - error TS: 2339 Property “Project” does not exist on type { } typescript 错误与属性“计数器”不存在类型“{}”.ts - typescript error with Property 'counter' does not exist on type '{}'.ts Typescript 错误:属性“status”在类型“never”上不存在。ts(2339) - Typescript error: Property 'status' does not exist on type 'never'.ts(2339)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM