A practical typeScript. React + Redux
Currently, the development of any modern front-end application is more complex than the level of 3r-3263. hello world The team is working on (the composition of which periodically changes) makes high demands on the quality of the code base. In order to maintain the quality level of the code at the proper level, we are in the front-line team of 3r3122. #gostgroup
We keep up with the times and are not afraid to apply modern technologies that show their practical benefits in projects 3r314. companies of all sizes
.
reference to the documentation and insert a piece of code from it as an illustration:
type TypeName
=
T extends string? "string":
T extends number? "number":
T extends boolean? "boolean":
T extends undefined? "undefined":
T extends Function? "function":
"object";
type T0 = TypeName
; //"string"
type T1 = TypeName <"a"> ; //"string"
type T2 = TypeName
; //"boolean"
type T3 = TypeName <() => void>; //"function"
type T4 = TypeName ; //"object"
How this feature helps in our case. Looking in 3r3104. type description
libraries react-redux
, you can find type InferableComponentEnhancerWithProps
which is responsible for ensuring that the types of injected properties do not fall into the external type of component properties, which we must explicitly specify when instantiating a component. In type 3r36332. InferableComponentEnhancerWithProps There are two generalized parameters: TInjectedProps
and TNeedsProps
. We are interested in the first. Let's try to pull this type out of this connector!
type TypeOfConnect = T extends InferableComponentEnhancerWithProps
? Props
: never
;
And directly type pulling on a real example from 3r3131. repository (which you can clone and run a test program there):
import React from 'react';
import {connect} from 'react-redux';
import {RootStore, init, TypeOfConnect, thunkAction, unboxThunk} from 'src /redux';
const storeEnhancer = connect (
(state: RootStore) => ({
state,
}), {
init,
thunkAction: unboxThunk (thunkAction),
} rrrr32: 32box323232.
type AppProps = {}
& TypeOfConnect
;
class App extends React.PureComponent {
componentDidMount () {
this.props.init ();
this.props.thunkAction (3000);
}
render () {
return (
<>
{this.props.a}
{this.props.b}
3rr3272.
{String (this .cn.art.r. 3r33272.);
}
}
export default storeEnhancer (App);
In the example above, we divide the connection to the repository (Redux) into two steps. In the first stage, we get a higher order component
storeEnhancer
(aka InferableComponentEnhancerWithProps
) to extract the injected property types from it using our helper type TypeOfConnect
and further combining (through the intersection of types 3r3–3263. & 3r3–3264.) the obtained property types with the property types of the component itself. In the second stage, we simply decorate our original component. Now, whatever you add to the connector, it will automatically fall into the component property types. Great, what we wanted to achieve!
The attentive reader noted that action generators (for brevity, hereafter, simplify to the term action) with side effects (thunk action creators) are further processed using the function 3r-3263. unboxThunk . What caused such an additional measure? Let's figure it out. First, we will look at the signature of such an action using the example of a program from the repository:
const thunkAction = (delay: number): ThunkAction => (dispatch) => {
console.log ('waiting for', delay);
setTimeout (() => {3r3323272. console.log ('reset');
dispatch (reset ());
}, delay);
};
As can be seen from the signature, our action does not immediately return the target function, but first intermediate, which is picked up by 3r-3263. redux-middleware to be able to produce side effects in our main function. But when using this function in the connected form in the component properties, the signature of this function is reduced, excluding the intermediate function. How to describe it in types? Need a special function converter. And again TypeScript shows its power. First we describe the type that removes the intermediate function from the signature:
CutMiddleFunction = T extends ( arg: infer Args) => ( args: any[]) => Infer R
? ( arg: Args) => R
: never
;
Here, in addition to the conditional types, a completely new innovation from TypeScript 3.0 is used, which allows you to display the type of an arbitrary (rest parameters) number of function arguments. See 3r33333 for details. documentation. . It now remains to cut out the extra part from our action in a rather rigid way:
const unboxThunk = ThunkAction ,
) => (
ThunkFn as any as CutMiddleFunction
);
Having skipped the action through such a converter, we have the required signature at the output. Now the action is ready for use in the connector.
So, by simple manipulations, we reduce our manual work when writing typed code on our stack. If you go a little further, you can also simplify the typing of action games and reducers, as we did in 3r33255. redux-modus .
P.S. When using dynamic action binding in the connector through the function and 3r-3263. redux.bindActionCreators we will need to take care of the more correct typing of this utility (perhaps by writing your own wrapper).
It may be interesting
weber
Author29-11-2018, 02:31
Publication DateReactJS / TypeScript
Category- Comments: 0
- Views: 392