Skip to content

Commit

Permalink
Converts a few hooks & helpers to Typescript (#1743)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vannevelj authored and timdorr committed Jun 29, 2021
1 parent 4f9c3b8 commit 76ed07f
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 18 deletions.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/hooks/useStore.js → src/hooks/useStore.ts
Expand Up @@ -14,7 +14,7 @@ export function createStoreHook(context = ReactReduxContext) {
? useDefaultReduxContext
: () => useContext(context)
return function useStore() {
const { store } = useReduxContext()
const { store } = useReduxContext()!
return store
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/batch.js → src/utils/batch.ts
@@ -1,12 +1,12 @@
// Default to a dummy "batch" implementation that just runs the callback
function defaultNoopBatch(callback) {
function defaultNoopBatch(callback: () => void) {
callback()
}

let batch = defaultNoopBatch

// Allow injecting another batching function later
export const setBatch = (newBatch) => (batch = newBatch)
export const setBatch = (newBatch: (callback: () => void) => void) => (batch = newBatch)

// Supply a getter just to skip dealing with ESM bindings
export const getBatch = () => batch
10 changes: 0 additions & 10 deletions src/utils/bindActionCreators.js

This file was deleted.

25 changes: 25 additions & 0 deletions src/utils/bindActionCreators.ts
@@ -0,0 +1,25 @@
import { ActionCreator, ActionCreatorsMapObject, AnyAction, Dispatch } from "redux"

function bindActionCreator<A extends AnyAction = AnyAction>(
actionCreator: ActionCreator<A>,
dispatch: Dispatch
) {
return function (this: any, ...args: any[]) {
return dispatch(actionCreator.apply(this, args))
}
}

export default function bindActionCreators(actionCreators: ActionCreator<any> | ActionCreatorsMapObject, dispatch: Dispatch) {
if (typeof actionCreators === 'function') {
return bindActionCreator(actionCreators, dispatch)
}

const boundActionCreators: ActionCreatorsMapObject = {}
for (const key in actionCreators) {
const actionCreator = actionCreators[key]
if (typeof actionCreator === 'function') {
boundActionCreators[key] = (...args) => dispatch(actionCreator(...args))
}
}
return boundActionCreators
}
2 changes: 1 addition & 1 deletion src/utils/isPlainObject.js → src/utils/isPlainObject.ts
Expand Up @@ -2,7 +2,7 @@
* @param {any} obj The object to inspect.
* @returns {boolean} True if the argument appears to be a plain object.
*/
export default function isPlainObject(obj) {
export default function isPlainObject(obj: unknown) {
if (typeof obj !== 'object' || obj === null) return false

let proto = Object.getPrototypeOf(obj)
Expand Down
4 changes: 2 additions & 2 deletions src/utils/shallowEqual.js → src/utils/shallowEqual.ts
@@ -1,12 +1,12 @@
function is(x, y) {
function is(x: unknown, y: unknown) {
if (x === y) {
return x !== 0 || y !== 0 || 1 / x === 1 / y
} else {
return x !== x && y !== y
}
}

export default function shallowEqual(objA, objB) {
export default function shallowEqual(objA: any, objB: any) {
if (is(objA, objB)) return true

if (
Expand Down
File renamed without changes.
@@ -1,7 +1,7 @@
import isPlainObject from './isPlainObject'
import warning from './warning'

export default function verifyPlainObject(value, displayName, methodName) {
export default function verifyPlainObject(value: unknown, displayName: string, methodName: string) {
if (!isPlainObject(value)) {
warning(
`${methodName}() in ${displayName} must return a plain object. Instead received ${value}.`
Expand Down
2 changes: 1 addition & 1 deletion src/utils/warning.js → src/utils/warning.ts
Expand Up @@ -4,7 +4,7 @@
* @param {String} message The warning message.
* @returns {void}
*/
export default function warning(message) {
export default function warning(message: string) {
/* eslint-disable no-console */
if (typeof console !== 'undefined' && typeof console.error === 'function') {
console.error(message)
Expand Down

0 comments on commit 76ed07f

Please sign in to comment.