1
1
import React , { Children , Component } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import { connect , Provider , ReactReduxContext } from 'react-redux' ;
4
- import instrument from 'redux-devtools-instrument' ;
4
+ import instrument , {
5
+ EnhancedStore ,
6
+ LiftedState ,
7
+ LiftedStore ,
8
+ Options ,
9
+ } from 'redux-devtools-instrument' ;
10
+ import { Action } from 'redux' ;
5
11
6
- function logError ( type ) {
7
- /* eslint-disable no-console */
12
+ function logError ( type : string ) {
8
13
if ( type === 'NoStore' ) {
9
14
console . error (
10
15
'Redux DevTools could not render. You must pass the Redux store ' +
@@ -18,16 +23,51 @@ function logError(type) {
18
23
'using createStore()?'
19
24
) ;
20
25
}
21
- /* eslint-enable no-console */
22
26
}
23
27
24
- export default function createDevTools ( children ) {
28
+ interface Props <
29
+ S ,
30
+ A extends Action < unknown > ,
31
+ MonitorState ,
32
+ MonitorAction extends Action < unknown >
33
+ > {
34
+ store ?: EnhancedStore < S , A , MonitorState > ;
35
+ }
36
+
37
+ type Monitor <
38
+ S ,
39
+ A extends Action < unknown > ,
40
+ MonitorProps ,
41
+ MonitorState ,
42
+ MonitorAction extends Action < unknown >
43
+ > = React . ReactElement <
44
+ MonitorProps ,
45
+ React . ComponentType < MonitorProps & LiftedState < S , A , MonitorState > > & {
46
+ update (
47
+ monitorProps : MonitorProps ,
48
+ state : MonitorState | undefined ,
49
+ action : MonitorAction
50
+ ) : MonitorState ;
51
+ }
52
+ > ;
53
+
54
+ export default function createDevTools <
55
+ S ,
56
+ A extends Action < unknown > ,
57
+ MonitorProps ,
58
+ MonitorState ,
59
+ MonitorAction extends Action < unknown >
60
+ > ( children : Monitor < S , A , MonitorProps , MonitorState , MonitorAction > ) {
25
61
const monitorElement = Children . only ( children ) ;
26
62
const monitorProps = monitorElement . props ;
27
63
const Monitor = monitorElement . type ;
28
- const ConnectedMonitor = connect ( ( state ) => state ) ( Monitor ) ;
64
+ const ConnectedMonitor = connect (
65
+ ( state : LiftedState < S , A , MonitorState > ) => state
66
+ ) ( Monitor as React . ComponentType < any > ) ;
29
67
30
- return class DevTools extends Component {
68
+ return class DevTools extends Component <
69
+ Props < S , A , MonitorState , MonitorAction >
70
+ > {
31
71
static contextTypes = {
32
72
store : PropTypes . object ,
33
73
} ;
@@ -36,13 +76,18 @@ export default function createDevTools(children) {
36
76
store : PropTypes . object ,
37
77
} ;
38
78
39
- static instrument = ( options ) =>
79
+ liftedStore ?: LiftedStore < S , A , MonitorState > ;
80
+
81
+ static instrument = ( options : Options < S , A , MonitorState , MonitorAction > ) =>
40
82
instrument (
41
83
( state , action ) => Monitor . update ( monitorProps , state , action ) ,
42
84
options
43
85
) ;
44
86
45
- constructor ( props , context ) {
87
+ constructor (
88
+ props : Props < S , A , MonitorState , MonitorAction > ,
89
+ context : { store ?: EnhancedStore < S , A , MonitorState > }
90
+ ) {
46
91
super ( props , context ) ;
47
92
48
93
if ( ReactReduxContext ) {
@@ -60,7 +105,7 @@ export default function createDevTools(children) {
60
105
if ( context . store ) {
61
106
this . liftedStore = context . store . liftedStore ;
62
107
} else {
63
- this . liftedStore = props . store . liftedStore ;
108
+ this . liftedStore = props . store ! . liftedStore ;
64
109
}
65
110
66
111
if ( ! this . liftedStore ) {
@@ -88,12 +133,23 @@ export default function createDevTools(children) {
88
133
logError ( 'NoStore' ) ;
89
134
return null ;
90
135
}
91
- if ( ! props . store . liftedStore ) {
136
+ if (
137
+ ! ( ( props . store as unknown ) as EnhancedStore < S , A , MonitorState > )
138
+ . liftedStore
139
+ ) {
92
140
logError ( 'NoLiftedStore' ) ;
93
141
return null ;
94
142
}
95
143
return (
96
- < Provider store = { props . store . liftedStore } >
144
+ < Provider
145
+ store = {
146
+ ( ( props . store as unknown ) as EnhancedStore <
147
+ S ,
148
+ A ,
149
+ MonitorState
150
+ > ) . liftedStore
151
+ }
152
+ >
97
153
< ConnectedMonitor { ...monitorProps } />
98
154
</ Provider >
99
155
) ;
0 commit comments