简体   繁体   English

Redux Toolkit slice with TypeScript expected 0 arguments, but got 1

[英]Redux Toolkit slice with TypeScript expected 0 arguments, but got 1

I have slice我有切片

const investment = createSlice({
  name: 'investments',
  initialState,
  reducers: {
    getInvestmentsRequest(state) {
      state.investments.status = RequestStatuses.loading;
    },
  }
})

And action is intercepted by middleware.动作被中间件拦截。 Middleware use payload.中间件使用负载。 But in slices I am not need use payload.但是在切片中我不需要使用有效负载。 If I set payload as second argument eslint will throw error with unused vars.如果我将有效负载设置为第二个参数,eslint 将抛出未使用变量的错误。 Caller code:调用者代码:

dispatch(getInvestmentsRequest({ term: product.term }));

And I have TS error Expected 0 arguments, but got 1.我有 TS 错误Expected 0 arguments, but got 1.

Anybody know how to resolve this typechecking conflict with TS, Redux Toolkit (slice) and redux-saga?有人知道如何解决与 TS、Redux Toolkit (slice) 和 redux-saga 的类型检查冲突吗?

If you have a good reason to add that second argument there (and you have, after all you want to specify a payload type for general type safety, even if it is not used in there), that eslint rule just does not make sense and actively hinders you from doing something useful.如果你有充分的理由在那里添加第二个参数(而且你有,毕竟你想要为一般类型安全指定一个有效负载类型,即使它没有在那里使用),那个 eslint 规则就没有意义并且积极地阻碍你做一些有用的事情。

And here is the point: you have active control over those eslint rules.这就是重点:您可以主动控制这些 eslint 规则。 It's your responsibility to maintain them in a way that makes sense to you.您有责任以对您有意义的方式维护它们。
And after Marie Kondo: if it does not spark joy, get rid of it.在 Marie Kondo 之后:如果它不能激发快乐,就摆脱它。

You can configure what the no-unused-vars rule marks as a linter error.您可以配置no-unused-vars规则标记为 linter 错误的内容。 In this case, you probably want to set the argsIgnorePattern to just not warn for any argument called action .在这种情况下,您可能希望将argsIgnorePattern设置为不警告任何名为action的参数。

Try it out by writing通过编写来尝试

/*eslint no-unused-vars: ["error", { "argsIgnorePattern": "^(_|action)" }]*/

at the top of your file, and when it does what you want move it over to your .eslintrc.js config file.在文件的顶部,当它执行您想要的操作时,将其移至您的.eslintrc.js配置文件。

As TypeScript checks for type safety, it won't allow us to pass arguments to functions without defining respective parameters.由于 TypeScript 检查类型安全,它不允许我们在未定义相应参数的情况下将 arguments 传递给函数。

getInvestmentsRequest(state, action) {
  state.investments.status = RequestStatuses.loading;
}

It is necessary to define action parameter if you are passing values to this function, otherwise typescript will throw error.如果要将值传递给此 function,则必须定义操作参数,否则 typescript 将抛出错误。 action parameter is an object which has a property on it called payload which points to the value that you are passing to this function. action 参数是一个 object,它有一个名为 payload 的属性,它指向你传递给这个 function 的值。

if you want to use the term value, you can do so by using the following:如果你想使用术语值,你可以使用以下方法:

state.investments.term = action.payload.term;

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

相关问题 React/Redux Toolkit TypeScript useDispatch 问题`Expected 0 arguments, but got 1` - React/Redux Toolkit TypeScript issue with useDispatch `Expected 0 arguments, but got 1` 预期为 0 arguments,但在 redux 和 typescript 中得到 1 - Expected 0 arguments, but got 1 in redux with typescript Redux 工具包预期为 0 arguments,但出现 1 个错误 - Redux Toolkit Expected 0 arguments, but got 1 error 预期为 0 arguments,但在将 arguments 传递给 redux 工具包商店中的异步函数时得到 1 - Expected 0 arguments, but got 1 while passing arguments to the async func in redux toolkit store React - 可重用 Redux 钩子切片问题(Redux Toolkit,Typescript) - React - reusable Redux hook slice issue (Redux Toolkit, Typescript) 是否可以对 Redux Toolkit Slice 的状态使用 typescript Union 类型? - Is is possible to use an typescript Union type for the state of a Redux Toolkit Slice? React-TypeScript:预期 0 个参数,但在 useReducer 上得到 1 个 - React-TypeScript: Expected 0 arguments, but got 1 on a useReducer Typescript 给我一个错误:预期为 0 arguments,但得到 1 - Typescript is giving me an error: Expected 0 arguments, but got 1 Typescript super() 上的错误:预期 1-2 arguments,但得到 0 - Typescript Error on super(): expected 1-2 arguments, but got 0 Typescript 和 redux 工具包 - Typescript and redux toolkit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM