为什么Redux.js“Dispatch”TypeScript接口有模板参数?(Why does the Redux.js “Dispatch” TypeScript interface have a

编程入门 行业动态 更新时间:2024-10-25 08:27:20
为什么Redux.js“Dispatch”TypeScript接口有模板参数?(Why does the Redux.js “Dispatch” TypeScript interface have a template parameter?)

相关线 。

如果界面如下:

export interface Dispatch<S> { <A extends Action>(action: A): A; }

......显然没有使用S ,为什么会出现?

我注意到S在其他地方用于“商店”相关的东西,但如果它没有在这个界面中使用,我不认为它应该在那里。

Relevant line.

If the interface is as follows:

export interface Dispatch<S> { <A extends Action>(action: A): A; }

...and S is clearly not used, then why is it there?

I notice that S is used elsewhere for "store" related things, but if it's not used in this interface, I don't think it should be there.

最满意答案

你是对的,就它本身而言似乎并没有多大意义。

我的猜测是他们已经完成它以确保在中间件中返回调度函数。

在像redux-thunk这样的中间件的情况下, 新的调度函数确实使用<S>参数,虽然我不确定如果在这种情况下默认值不重要。

Having state type parameter S allows middlewares be more accurate in typings. For example, thunk function takes a dispatch and a getState(): S, so without S it couldn't be well-typed. See the example of this here below https://github.com/Igorbek/redux/blob/ts-def-improv/test/typescript/dispatch.ts#L9-L16

Source

Credit goes to @Lucas for finding that source.

更多推荐

本文发布于:2023-08-01 20:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1362845.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接口   模板   参数   Dispatch   Redux

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!