Skip to content

Commit 0934852

Browse files
committed
fix: modify types
1 parent 490e464 commit 0934852

File tree

5 files changed

+52
-33
lines changed

5 files changed

+52
-33
lines changed

src/components/Provider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface ProviderProps<A extends Action = AnyAction> {
1515
* If this is used, generate own connect HOC by using connectAdvanced, supplying the same context provided to the
1616
* Provider. Initial value doesn't matter, as it is overwritten with the internal state of Provider.
1717
*/
18-
context?: Context<ReactReduxContextValue>
18+
context?: Context<ReactReduxContextValue | null>
1919
children: ReactNode
2020
}
2121

src/connect/wrapMapToProps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function wrapMapToPropsConstant(
4747
// A length of zero is assumed to mean mapToProps is getting args via arguments or ...args and
4848
// therefore not reporting its length accurately..
4949
export function getDependsOnOwnProps(mapToProps: MapToProps) {
50-
return mapToProps?.dependsOnOwnProps
50+
return mapToProps.dependsOnOwnProps
5151
? Boolean(mapToProps.dependsOnOwnProps)
5252
: mapToProps.length !== 1
5353
}

test/components/hooks.spec.tsx

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('React', () => {
4343
const mapStateSpy1 = jest.fn()
4444
const renderSpy1 = jest.fn()
4545

46-
let component1StateList
46+
let component1StateList: number[]
4747

4848
const component1Decorator = connect<
4949
Omit<RootStateType, 'byId'>,
@@ -77,23 +77,31 @@ describe('React', () => {
7777
const mapStateSpy2 = jest.fn()
7878
const renderSpy2 = jest.fn()
7979

80-
interface Component2PropsType {
81-
mappedProp: Array<string>
80+
interface Component2Tstate {
81+
mappedProp: string[]
8282
}
8383
const component2Decorator = connect<
84-
Component2PropsType,
84+
Component2Tstate,
8585
unknown,
8686
Omit<RootStateType, 'byId'>,
8787
RootStateType
88-
>((state, ownProps) => {
89-
mapStateSpy2()
90-
91-
return {
92-
mappedProp: ownProps.list.map((id) => state.byId[id]),
88+
>(
89+
(
90+
state: RootStateType,
91+
ownProps: Omit<RootStateType, 'byId'>
92+
): Component2Tstate => {
93+
mapStateSpy2()
94+
95+
return {
96+
mappedProp: ownProps.list.map((id) => state.byId[id]),
97+
}
9398
}
94-
})
99+
)
100+
interface Component2PropsType {
101+
list: number[]
102+
}
95103

96-
const component2 = (props) => {
104+
const component2 = (props: Component2PropsType) => {
97105
renderSpy2()
98106

99107
expect(props.list).toBe(component1StateList)

test/hooks/useDispatch.spec.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import {
77
createDispatchHook,
88
} from '../../src/index'
99
import type { ProviderProps } from '../../src/'
10+
import type { ReactReduxContextValue } from '../../src/components/Context'
1011

11-
const store = createStore((c: number): number => c + 1)
12-
const store2 = createStore((c: number): number => c + 2)
12+
const store = createStore((c: number = 1): number => c + 1)
13+
const store2 = createStore((c: number = 1): number => c + 2)
1314

1415
describe('React', () => {
1516
describe('hooks', () => {
@@ -27,7 +28,8 @@ describe('React', () => {
2728
})
2829
describe('createDispatchHook', () => {
2930
it("returns the correct store's dispatch function", () => {
30-
const nestedContext = React.createContext(null)
31+
const nestedContext =
32+
React.createContext<ReactReduxContextValue | null>(null)
3133
const useCustomDispatch = createDispatchHook(nestedContext)
3234
const { result } = renderHook(() => useDispatch(), {
3335
// eslint-disable-next-line react/prop-types

test/hooks/useSelector.spec.tsx

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ import {
1111
connect,
1212
createSelectorHook,
1313
} from '../../src/index'
14-
import type { FunctionComponent } from 'react'
1514
import { useReduxContext } from '../../src/hooks/useReduxContext'
15+
import type { FunctionComponent, DispatchWithoutAction, ReactNode } from 'react'
1616
import type { Store, AnyAction } from 'redux'
1717
import type { ProviderProps, TypedUseSelectorHook } from '../../src/'
1818
import type { Subscription } from '../../src/utils/Subscription'
19+
import type { ReactReduxContextValue } from '../../src/components/Context'
1920

2021
describe('React', () => {
2122
describe('hooks', () => {
@@ -24,7 +25,7 @@ describe('React', () => {
2425
count: number
2526
}
2627
let normalStore: Store<NormalStateType, AnyAction>
27-
let renderedItems = []
28+
let renderedItems: any[] = []
2829
type RootState = ReturnType<typeof normalStore.getState>
2930
let useNormalSelector: TypedUseSelectorHook<RootState> = useSelector
3031

@@ -80,7 +81,7 @@ describe('React', () => {
8081

8182
describe('lifecycle interactions', () => {
8283
it('always uses the latest state', () => {
83-
const store = createStore((c: number): number => c + 1, -1)
84+
const store = createStore((c: number = 1): number => c + 1, -1)
8485

8586
const Comp = () => {
8687
const selector = useCallback((c: number): number => c + 1, [])
@@ -106,7 +107,7 @@ describe('React', () => {
106107
let rootSubscription: Subscription
107108

108109
const Parent = () => {
109-
const { subscription } = useReduxContext()
110+
const { subscription } = useReduxContext() as ReactReduxContextValue
110111
rootSubscription = subscription
111112
const count = useNormalSelector((s) => s.count)
112113
return count === 1 ? <Child /> : null
@@ -122,19 +123,19 @@ describe('React', () => {
122123
<Parent />
123124
</ProviderMock>
124125
)
125-
126+
// @ts-ignore ts(2454)
126127
expect(rootSubscription.getListeners().get().length).toBe(1)
127128

128129
normalStore.dispatch({ type: '' })
129-
130+
// @ts-ignore ts(2454)
130131
expect(rootSubscription.getListeners().get().length).toBe(2)
131132
})
132133

133134
it('unsubscribes when the component is unmounted', () => {
134135
let rootSubscription: Subscription
135136

136137
const Parent = () => {
137-
const { subscription } = useReduxContext()
138+
const { subscription } = useReduxContext() as ReactReduxContextValue
138139
rootSubscription = subscription
139140
const count = useNormalSelector((s) => s.count)
140141
return count === 0 ? <Child /> : null
@@ -150,11 +151,11 @@ describe('React', () => {
150151
<Parent />
151152
</ProviderMock>
152153
)
153-
154+
// @ts-ignore ts(2454)
154155
expect(rootSubscription.getListeners().get().length).toBe(2)
155156

156157
normalStore.dispatch({ type: '' })
157-
158+
// @ts-ignore ts(2454)
158159
expect(rootSubscription.getListeners().get().length).toBe(1)
159160
})
160161

@@ -183,7 +184,7 @@ describe('React', () => {
183184
})
184185

185186
it('works properly with memoized selector with dispatch in Child useLayoutEffect', () => {
186-
const store = createStore((c: number): number => c + 1, -1)
187+
const store = createStore((c: number = 1): number => c + 1, -1)
187188

188189
const Comp = () => {
189190
const selector = useCallback((c: number): number => c, [])
@@ -282,7 +283,7 @@ describe('React', () => {
282283

283284
it('uses the latest selector', () => {
284285
let selectorId = 0
285-
let forceRender
286+
let forceRender: DispatchWithoutAction
286287

287288
const Comp = () => {
288289
const [, f] = useReducer((c) => c + 1, 0)
@@ -301,15 +302,19 @@ describe('React', () => {
301302

302303
expect(renderedItems).toEqual([0])
303304

304-
rtl.act(forceRender)
305+
rtl.act(() => {
306+
forceRender()
307+
})
305308
expect(renderedItems).toEqual([0, 1])
306309

307310
rtl.act(() => {
308311
normalStore.dispatch({ type: '' })
309312
})
310313
expect(renderedItems).toEqual([0, 1])
311314

312-
rtl.act(forceRender)
315+
rtl.act(() => {
316+
forceRender()
317+
})
313318
expect(renderedItems).toEqual([0, 1, 2])
314319
})
315320

@@ -503,8 +508,8 @@ describe('React', () => {
503508
})
504509

505510
describe('createSelectorHook', () => {
506-
let defaultStore
507-
let customStore
511+
let defaultStore: Store
512+
let customStore: Store
508513
type StateType = {
509514
count: number
510515
}
@@ -519,7 +524,8 @@ describe('React', () => {
519524
})
520525

521526
it('subscribes to the correct store', () => {
522-
const nestedContext = React.createContext(null)
527+
const nestedContext =
528+
React.createContext<ReactReduxContextValue | null>(null)
523529
const useCustomSelector = createSelectorHook(nestedContext)
524530
let defaultCount = null
525531
let customCount = null
@@ -531,7 +537,10 @@ describe('React', () => {
531537
defaultCount = count
532538
return <>{children}</>
533539
}
534-
const DisplayCustomCount = ({ children = null }) => {
540+
interface DisplayCustomCountType {
541+
children: ReactNode
542+
}
543+
const DisplayCustomCount = ({ children }: DisplayCustomCountType) => {
535544
const count = useCustomSelector<StateType>(getCount)
536545
customCount = count
537546
return <>{children}</>

0 commit comments

Comments
 (0)