Skip to content

Commit 5004e46

Browse files
authored
Converts a few hooks & helpers to Typescript (#1743)
1 parent 0a29990 commit 5004e46

12 files changed

+33
-18
lines changed
File renamed without changes.
File renamed without changes.

src/hooks/useStore.js renamed to src/hooks/useStore.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function createStoreHook(context = ReactReduxContext) {
1414
? useDefaultReduxContext
1515
: () => useContext(context)
1616
return function useStore() {
17-
const { store } = useReduxContext()
17+
const { store } = useReduxContext()!
1818
return store
1919
}
2020
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Default to a dummy "batch" implementation that just runs the callback
2-
function defaultNoopBatch(callback) {
2+
function defaultNoopBatch(callback: () => void) {
33
callback()
44
}
55

66
let batch = defaultNoopBatch
77

88
// Allow injecting another batching function later
9-
export const setBatch = (newBatch) => (batch = newBatch)
9+
export const setBatch = (newBatch: (callback: () => void) => void) => (batch = newBatch)
1010

1111
// Supply a getter just to skip dealing with ESM bindings
1212
export const getBatch = () => batch

src/utils/bindActionCreators.js

-10
This file was deleted.

src/utils/bindActionCreators.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ActionCreator, ActionCreatorsMapObject, AnyAction, Dispatch } from "redux"
2+
3+
function bindActionCreator<A extends AnyAction = AnyAction>(
4+
actionCreator: ActionCreator<A>,
5+
dispatch: Dispatch
6+
) {
7+
return function (this: any, ...args: any[]) {
8+
return dispatch(actionCreator.apply(this, args))
9+
}
10+
}
11+
12+
export default function bindActionCreators(actionCreators: ActionCreator<any> | ActionCreatorsMapObject, dispatch: Dispatch) {
13+
if (typeof actionCreators === 'function') {
14+
return bindActionCreator(actionCreators, dispatch)
15+
}
16+
17+
const boundActionCreators: ActionCreatorsMapObject = {}
18+
for (const key in actionCreators) {
19+
const actionCreator = actionCreators[key]
20+
if (typeof actionCreator === 'function') {
21+
boundActionCreators[key] = (...args) => dispatch(actionCreator(...args))
22+
}
23+
}
24+
return boundActionCreators
25+
}

src/utils/isPlainObject.js renamed to src/utils/isPlainObject.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @param {any} obj The object to inspect.
33
* @returns {boolean} True if the argument appears to be a plain object.
44
*/
5-
export default function isPlainObject(obj) {
5+
export default function isPlainObject(obj: unknown) {
66
if (typeof obj !== 'object' || obj === null) return false
77

88
let proto = Object.getPrototypeOf(obj)

src/utils/shallowEqual.js renamed to src/utils/shallowEqual.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
function is(x, y) {
1+
function is(x: unknown, y: unknown) {
22
if (x === y) {
33
return x !== 0 || y !== 0 || 1 / x === 1 / y
44
} else {
55
return x !== x && y !== y
66
}
77
}
88

9-
export default function shallowEqual(objA, objB) {
9+
export default function shallowEqual(objA: any, objB: any) {
1010
if (is(objA, objB)) return true
1111

1212
if (

src/utils/verifyPlainObject.js renamed to src/utils/verifyPlainObject.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import isPlainObject from './isPlainObject'
22
import warning from './warning'
33

4-
export default function verifyPlainObject(value, displayName, methodName) {
4+
export default function verifyPlainObject(value: unknown, displayName: string, methodName: string) {
55
if (!isPlainObject(value)) {
66
warning(
77
`${methodName}() in ${displayName} must return a plain object. Instead received ${value}.`

src/utils/warning.js renamed to src/utils/warning.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @param {String} message The warning message.
55
* @returns {void}
66
*/
7-
export default function warning(message) {
7+
export default function warning(message: string) {
88
/* eslint-disable no-console */
99
if (typeof console !== 'undefined' && typeof console.error === 'function') {
1010
console.error(message)

0 commit comments

Comments
 (0)