当前位置:网站首页>How to type Redux actions and Redux reducers in TypeScript?
How to type Redux actions and Redux reducers in TypeScript?
2022-06-23 01:36:00 【Superior virtue and weak water】
problem :
What is the best way to cast the action parameter in a redux reducer with typescript? On the redux reducer Medium conversion action What is the best way to use parameters ?There will be multiple action interfaces that can occur that all extend a base interface with a property type. There may be multiple operation interfaces , They all extend the basic interface with attribute types .The extended action interfaces can have more properties that are all different between the action interfaces. The extended action interface can have more properties , These properties are different between action interfaces .Here is an example below: Here is an example :
interface IAction { type: string}interface IActionA extends IAction { a: string}interface IActionB extends IAction { b: string}const reducer = (action: IAction) { switch (action.type) { case 'a': return console.info('action a: ', action.a) // property 'a' does not exists on type IAction case 'b': return console.info('action b: ', action.b) // property 'b' does not exists on type IAction }}The problem is that action needs to be cast as a type that has access to both IActionA and IActionB so the reducer can use both action.a and action.a without throwing an error. The problem is action Need to be converted to be accessible IActionA and IActionB The type of , such reducer You can use it at the same time action.a and action.a Instead of throwing errors .
I have several ideas how to work around this issue: I have a few ideas on how to solve this problem :
- Cast
actiontoany. takeactionProject toany. - Use optional interface members. Use optional interface members .
example: Example :
interface IAction { type: string a?: string b?: string}- Use different reducers for every action type. Use different reducers for each action type .
What is the best way to organize Action/Reducers in typescript? Organize in typescript Action/Reducers What's the best way to ?Thank you in advance! Thank you first !
Solution :
Reference resources : https://stackoom.com/en/question/2OsYD边栏推荐
- Why can't I access object properties in a for in loop in an object array
- There is no corresponding change on the page after the code runs at the Chrome browser break point
- Module 8 job
- SQL programming task03 job - more complex query
- On AI and its future trend | community essay solicitation
- Do you know the memory components of MySQL InnoDB?
- 崔鹏团队:万字长文梳理「稳定学习」全景图
- Webdriver and selenium Usage Summary
- Pat class A - 1007 maximum subsequence sum
- JS image resolution compression
猜你喜欢
![[sliding window] leetcode992 Subarrays with K Different Integers](/img/69/1ac0c54d33af0f7a9e3db3e82d076b.png)
[sliding window] leetcode992 Subarrays with K Different Integers

Webdriver and selenium Usage Summary

Autumn move script B
![[Title Fine brushing] 2023 Hesai FPGA](/img/e8/d9d8c549a4fee529b69ebfc9a9d6ef.png)
[Title Fine brushing] 2023 Hesai FPGA

SFOD:无源域适配升级优化,让检测模型更容易适应新数据

Explain the startup process of opengauss multithreading architecture in detail

The road of architects starts from "storage selection"

Do you know the memory components of MySQL InnoDB?

Psychological analysis of the safest spot Silver

JMeter associated login 302 type interface
随机推荐
[launch] redis Series 2: data persistence to improve availability
Get the direction of mouse movement
MySQL-Seconds_ behind_ Master accuracy error
Xiaobai operates win10 to expand Disk C (allocate disk D memory to Disk C) and the test is valid for many times
SQL programming task05 job -sql advanced processing
Explain the startup process of opengauss multithreading architecture in detail
E-R图
JMeter associated login 302 type interface
崔鹏团队:万字长文梳理「稳定学习」全景图
[hdu] P7079 Pty loves lines
Analysis of current mainstream video coding technology | community essay solicitation
E-R diagram
B tree and b+ tree
There is no corresponding change on the page after the code runs at the Chrome browser break point
leetcode 91. Decode ways (medium)
Day500: keyboard line
SAP ui5 application development tutorial 102 - detailed explanation of the print function of SAP ui5 applications
C language student achievement ranking system
Use elk to save syslog, NetFlow logs and audit network interface traffic
关于打算做一个web的问题看板,需要使用哪些方面语言及数据库知识!
