当前位置:网站首页>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边栏推荐
- [UVM] don't say that your VIP can't use ral model
- Debian10 installing zabbix5.4
- 3DMAX modeling notes (I): introducing 3DMAX and creating the first model Hello World
- 3D打印微组织
- Js--- SVG to png
- 魔王冷饭||#099 魔王说西游;老板的本质;再答中年危机;专业选择
- Day367: valid complete square
- C# SerializableDictionary序列化/反序列化
- Detailed explanation of clip attribute parameters
- Swiftui swift tutorial 14 useful array operators
猜你喜欢

基于深度学习的视觉目标检测技术综述

Installing MySQL for Linux

Read Amazon memorydb database based on redis

Installation record of ros1noetic in Win 11

LeetCode 206. Reverse linked list (iteration + recursion)

07 project cost management

office2016+visio2016

On AI and its future trend | community essay solicitation

three. JS simulated driving tour art exhibition hall - creating super camera controller

SQL programming task02 job - basic query and sorting
随机推荐
Day500: keyboard line
Ros2 summer school 2022 transfer-
Error reported when compiling basalt
SAP mm transaction code vl04 create outbound delivery for sto
SQL programming task03 job - more complex query
Philosopher's walk gym divide and conquer + fractal
New progress in the construction of meituan's Flink based real-time data warehouse platform
Pat class A - 1015 reversible primes
JS to determine whether the browser has opened the console
ERROR { err: YAMLException: end of the stream or a document separator is expected at line 6, colum
three. JS simulated driving tour art exhibition hall - creating super camera controller
Debian10 configuring rsyslog+loganalyzer log server
Prevent others from using the browser to debug
Installing MySQL for Linux
SQL programming task05 job -sql advanced processing
Debian10 LVM logical volumes
Pat class A - 1007 maximum subsequence sum
Charles garbled code problem solving
B tree and b+ tree
MySQL - SQL execution process
