Skip to content

Commit

Permalink
chore: enhance TS types, add missed export explicit type definition
Browse files Browse the repository at this point in the history
  • Loading branch information
vikiboss committed Dec 2, 2024
1 parent e79df57 commit 9a07cf7
Show file tree
Hide file tree
Showing 31 changed files with 95 additions and 40 deletions.
4 changes: 2 additions & 2 deletions packages/react-use/src/use-async-effect/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect } from 'react'
import { createAsyncEffect } from '../utils/create-effect/async'

import type { AsyncEffectCallback } from '../utils/create-effect/async'
import type { AsyncEffectCallback, UseAsyncEffect } from '../utils/create-effect/async'

export type UseAsyncEffectCallback = AsyncEffectCallback

Expand All @@ -11,4 +11,4 @@ export type UseAsyncEffectCallback = AsyncEffectCallback
* @param {AsyncEffectCallback} asyncEffectCallback - `AsyncEffectCallback`, a callback function for async effect, see {@link AsyncEffectCallback}
* @param {DependencyList} [deps] - `DependencyList`, an array of dependencies, see [React.useEffect](https://react.dev/reference/react/useEffect)
*/
export const useAsyncEffect = createAsyncEffect(useEffect)
export const useAsyncEffect: UseAsyncEffect<never> = createAsyncEffect<never>(useEffect)
4 changes: 2 additions & 2 deletions packages/react-use/src/use-async-update-effect/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useUpdateEffect } from '../use-update-effect'
import { createAsyncEffect } from '../utils/create-effect/async'

import type { AsyncEffectCallback } from '../utils/create-effect/async'
import type { AsyncEffectCallback, UseAsyncEffect } from '../utils/create-effect/async'

export interface UseAsyncUpdateEffectCallback extends AsyncEffectCallback {}

Expand All @@ -16,4 +16,4 @@ export interface UseAsyncUpdateEffectCallback extends AsyncEffectCallback {}
* @param {DependencyList} [deps] - `DependencyList`, an array of dependencies, see [React.useEffect](https://react.dev/reference/react/useEffect)
*
*/
export const useAsyncUpdateEffect = createAsyncEffect(useUpdateEffect)
export const useAsyncUpdateEffect: UseAsyncEffect<never> = createAsyncEffect(useUpdateEffect)
3 changes: 2 additions & 1 deletion packages/react-use/src/use-debounced-effect/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useEffect } from 'react'
import { createDebouncedEffect } from '../utils/create-effect/debounced'

import type { UseDebouncedEffect } from '../utils/create-effect/debounced'
import type { DebounceOptions } from '../utils/debounce'

export interface UseDebouncedEffectOptions extends DebounceOptions {}

/**
* A React Hook like [React.useEffect](https://react.dev/reference/react/useEffect), but debounced.
*/
export const useDebouncedEffect = createDebouncedEffect(useEffect)
export const useDebouncedEffect: UseDebouncedEffect<never> = createDebouncedEffect(useEffect)
4 changes: 3 additions & 1 deletion packages/react-use/src/use-deep-compare-effect/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useEffect } from 'react'
import { createDeepCompareEffect } from '../utils/create-effect/deep-compare'

import type { UseDeepCompareEffect } from '../utils/create-effect/deep-compare'

/**
* A React Hook like [React.useEffect](https://react.dev/reference/react/useEffect), but with [deep comparison](https://github.com/sheinsight/react-use/blob/main/src/utils/equal.ts#L29) of dependencies.
*/
export const useDeepCompareEffect = createDeepCompareEffect(useEffect)
export const useDeepCompareEffect: UseDeepCompareEffect<never> = createDeepCompareEffect(useEffect)
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { useIsomorphicLayoutEffect } from '../use-isomorphic-layout-effect'
import { createDeepCompareEffect } from '../utils/create-effect/deep-compare'

import type { UseDeepCompareEffect } from '../utils/create-effect/deep-compare'

/**
* Same as [useDeepCompareEffect](https://sheinsight.github.io/react-use/reference/use-deep-compare-effect), but use `useLayoutEffect` instead of `useEffect`.
*/
export const useDeepCompareLayoutEffect = createDeepCompareEffect(useIsomorphicLayoutEffect)
export const useDeepCompareLayoutEffect: UseDeepCompareEffect<never> =
createDeepCompareEffect(useIsomorphicLayoutEffect)
2 changes: 1 addition & 1 deletion packages/react-use/src/use-document-leave/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface UseDocumentLeaveEvent {
/**
* A React Hook that tracks whether the user is leaving the document.
*/
export function useDocumentLeave() {
export function useDocumentLeave(): boolean {
const [isLeft, setIsLeft] = useSafeState(false)

function handler(event: MouseEvent) {
Expand Down
4 changes: 3 additions & 1 deletion packages/react-use/src/use-effect-once/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useEffect } from 'react'
import { createEffectOnce } from '../utils/create-effect/once'

import type { UseEffectOnce } from '../utils/create-effect/once'

/**
* A React Hook like [React.useEffect](https://react.dev/reference/react/useEffect) but only run once, with more **semantic** name.
*
* It's merely an alias to `useEffect` with an empty dependency array.
*/
export const useEffectOnce = createEffectOnce(useEffect)
export const useEffectOnce: UseEffectOnce<never> = createEffectOnce(useEffect)
5 changes: 4 additions & 1 deletion packages/react-use/src/use-event-bus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export interface UseEventBusOptions {
autoCleanup?: boolean
}

export const events = /* #__PURE__ */ new Map<EventBusIdentifier, EventBusEvents<any>>()
export const events: Map<EventBusIdentifier, EventBusEvents<any>> = /* #__PURE__ */ new Map<
EventBusIdentifier,
EventBusEvents<any>
>()

/**
* A React Hook that provides a simple event bus for your application.
Expand Down
2 changes: 1 addition & 1 deletion packages/react-use/src/use-first-render/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useRef } from 'react'
/**
* A React Hook for determining whether the component is currently in its initial render.
*/
export function useFirstRender() {
export function useFirstRender(): boolean {
const firstRenderRef = useRef(true)

if (firstRenderRef.current) {
Expand Down
4 changes: 3 additions & 1 deletion packages/react-use/src/use-isomorphic-layout-effect/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useEffect, useLayoutEffect } from 'react'
import { isClient } from '../utils/basic'

export type UseEffect = typeof useEffect

/**
* A React Hook that will automatically use [React.useLayoutEffect](https://react.dev/reference/react/useLayoutEffect) in client side,
*
* and [React.useEffect](https://react.dev/reference/react/useEffect) in server side to avoid the warning of `useLayoutEffect` in Server-side Rendering (SSR).
*/
export const useIsomorphicLayoutEffect = isClient ? useLayoutEffect : useEffect
export const useIsomorphicLayoutEffect: UseEffect = isClient ? useLayoutEffect : useEffect
4 changes: 3 additions & 1 deletion packages/react-use/src/use-layout-effect-once/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useIsomorphicLayoutEffect } from '../use-isomorphic-layout-effect'
import { createEffectOnce } from '../utils/create-effect/once'

import type { UseEffectOnce } from '../utils/create-effect/once'

/**
* A React Hook that like <Link to="/reference/use-effect-once">`useEffectOnce`</Link> but use <Link to="/reference/use-isomorphic-layout-effect">`useIsomorphicLayoutEffect`</Link> under the hood.
*/
export const useLayoutEffectOnce = createEffectOnce(useIsomorphicLayoutEffect)
export const useLayoutEffectOnce: UseEffectOnce<never> = createEffectOnce(useIsomorphicLayoutEffect)
4 changes: 2 additions & 2 deletions packages/react-use/src/use-query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import type { UseThrottledFnOptions } from '../use-throttled-fn'
import type { AnyFunc, Gettable, Promisable } from '../utils/basic'
import type { UseQueryCacheLike } from './use-query-cache'

export const defaultIsVisible = () => !document.hidden
export const defaultIsOnline = () => navigator.onLine
export const defaultIsVisible: () => boolean = () => !document.hidden
export const defaultIsOnline: () => boolean = () => navigator.onLine

export { mutate } from './use-query-cache'

Expand Down
6 changes: 3 additions & 3 deletions packages/react-use/src/use-re-connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ export interface UseReConnectOptions {
registerReConnect?: (callback: AnyFunc) => void
}

export function registerWebReConnect(callback: AnyFunc) {
export function registerWebReConnect(callback: AnyFunc): () => void {
function handleOnline() {
callback()
}

window.addEventListener('online', handleOnline, { passive: true })

return () => {
return (): void => {
window.removeEventListener('online', handleOnline)
}
}
Expand All @@ -27,7 +27,7 @@ export function registerWebReConnect(callback: AnyFunc) {
*
* @since 1.4.0
*/
export function useReConnect(callback: AnyFunc, options: UseReConnectOptions = {}) {
export function useReConnect(callback: AnyFunc, options: UseReConnectOptions = {}): void {
const { registerReConnect = registerWebReConnect } = options

const latest = useLatest({ callback })
Expand Down
3 changes: 2 additions & 1 deletion packages/react-use/src/use-throttled-effect/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useEffect } from 'react'
import { createThrottledEffect } from '../utils/create-effect/throttled'

import type { UseThrottledEffect } from '../utils/create-effect/throttled'
import type { ThrottleOptions } from '../utils/throttle'

export interface UseThrottledEffectOptions extends ThrottleOptions {}

/**
* A React Hook like [React.useEffect](https://react.dev/reference/react/useEffect), but throttled.
*/
export const useThrottledEffect = createThrottledEffect(useEffect)
export const useThrottledEffect: UseThrottledEffect<never> = createThrottledEffect(useEffect)
2 changes: 1 addition & 1 deletion packages/react-use/src/use-title/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type UseTitleOptions = {
/**
* A React Hook that helps to manage the title of the document.
*/
export function useTitle(newTitle: Gettable<string>, options: UseTitleOptions = {}) {
export function useTitle(newTitle: Gettable<string>, options: UseTitleOptions = {}): () => void {
const [originalTitleRef, originalTitle] = useGetterRef('')
const { template = '%s', restoreOnUnmount = false } = options

Expand Down
2 changes: 1 addition & 1 deletion packages/react-use/src/use-tracked-effect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type TrackedEffectCallback = (depIndexes: number[], previousDeps?: Deps,
/**
* A React Hook for detecting dependencies that have **actually changed** between renders, only run in development mode.
*/
export function useTrackedEffect(callback: TrackedEffectCallback, deps?: Deps) {
export function useTrackedEffect(callback: TrackedEffectCallback, deps?: Deps): void {
const previousDepsRef = useRef<Deps>()
const latest = useLatest({ callback })

Expand Down
17 changes: 16 additions & 1 deletion packages/react-use/src/use-tracked-ref-state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,22 @@ import { shallowEqual } from '../utils/equal'

export type UseTrackedRefStateRefState<S> = { [K in keyof S]: { used: boolean; value: S[K] } }

export function useTrackedRefState<S extends object, Keys extends keyof S = keyof S>(refState: S) {
export type UseTrackedRefStateReturns<S extends object, Keys extends keyof S = keyof S> = readonly [
S,
{
updateRefState: <K extends Keys>(
key: K,
newValue: S[K],
compare?: (prevData: S[K], nextData: S[K]) => boolean,
) => void
markKeyAsUsed: <K extends Keys>(key: K) => void
},
UseTrackedRefStateRefState<S>,
]

export function useTrackedRefState<S extends object, Keys extends keyof S = keyof S>(
refState: S,
): UseTrackedRefStateReturns<S, Keys> {
const render = useRender()

const stateRef = useRef(
Expand Down
2 changes: 1 addition & 1 deletion packages/react-use/src/use-unmount/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { AnyFunc } from '../utils/basic'
/**
* A React Hook that helps to run a callback when the component unmounts.
*/
export function useUnmount(callback?: AnyFunc | null | undefined | false) {
export function useUnmount(callback?: AnyFunc | null | undefined | false): void {
const latest = useLatest(callback)

useEffectOnce(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-use/src/use-unmounted-ref/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useEffectOnce } from '../use-effect-once'
/**
* A React Hook that helps to create a ref that can be checked if the component is unmounted.
*/
export function useUnmountedRef() {
export function useUnmountedRef(): React.MutableRefObject<boolean> {
const unmountedRef = useRef(false)

useEffectOnce(() => {
Expand Down
4 changes: 3 additions & 1 deletion packages/react-use/src/use-update-effect/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useEffect } from 'react'
import { createUpdateEffect } from '../utils/create-effect/update'

import type { UseUpdateEffect } from '../utils/create-effect/update'

/**
* A React Hook like [React.useEffect](https://react.dev/reference/react/useEffect), but ignore the first invocation on mount.
*/
export const useUpdateEffect = createUpdateEffect(useEffect)
export const useUpdateEffect: UseUpdateEffect<never> = createUpdateEffect(useEffect)
4 changes: 3 additions & 1 deletion packages/react-use/src/use-update-layout-effect/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { useIsomorphicLayoutEffect } from '../use-isomorphic-layout-effect'
import { createUpdateEffect } from '../utils/create-effect/update'

import type { UseUpdateEffect } from '../utils/create-effect/update'

/**
* A React Hook like [React.useLayoutEffect](https://react.dev/reference/react/useLayoutEffect),
* but ignore the first invocation on mount, and use `useIsomorphicLayoutEffect` under the hood to support Server-side Rendering (SSR).
*/
export const useUpdateLayoutEffect = createUpdateEffect(useIsomorphicLayoutEffect)
export const useUpdateLayoutEffect: UseUpdateEffect<never> = createUpdateEffect(useIsomorphicLayoutEffect)
6 changes: 3 additions & 3 deletions packages/react-use/src/utils/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ declare const process: {
}

// build tools are expected to replace this value when building for production
export const isDev = process.env.NODE_ENV !== 'production'
export const isDev: boolean = process.env.NODE_ENV !== 'production'

// useful in Server-side Rendering (SSR)
/* v8 ignore next */
export const isClient = Boolean(typeof window !== 'undefined' && isFunction(window?.document?.createElement))
export const isClient: boolean = Boolean(typeof window !== 'undefined' && isFunction(window?.document?.createElement))

export function noop(): undefined {}

Expand Down Expand Up @@ -159,7 +159,7 @@ export function increaseWithUnit(target: string | number, delta: number) {
return result + unit
}

export function createSingletonPromise<T>(fn: () => Promise<T>) {
export function createSingletonPromise<T>(fn: () => Promise<T>): (() => Promise<T>) & { reset: () => Promise<void> } {
const promise = {
current: undefined as Promise<T> | undefined,
}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-use/src/utils/create-effect/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { ExtendedReactEffect } from '../basic'
*/
export type AsyncEffectCallback = (isCancelled: () => boolean) => void

type UseAsyncEffect<T = unknown> = (callback: AsyncEffectCallback, deps?: DependencyList, ...args: T[]) => void
export type UseAsyncEffect<T = unknown> = (callback: AsyncEffectCallback, deps?: DependencyList, ...args: T[]) => void

export function createAsyncEffect<T = unknown>(effect: ExtendedReactEffect<T>): UseAsyncEffect<T> {
return (callback: AsyncEffectCallback, deps?: DependencyList, ...args: T[]): void => {
Expand Down
9 changes: 8 additions & 1 deletion packages/react-use/src/utils/create-effect/debounced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import type { DependencyList, EffectCallback } from 'react'
import type { ExtendedReactEffect } from '../basic'
import type { DebounceOptions } from '../debounce'

export function createDebouncedEffect<T = unknown>(effect: ExtendedReactEffect<T>) {
export type UseDebouncedEffect<T> = (
callback: EffectCallback,
deps?: DependencyList,
options?: DebounceOptions,
...args: T[]
) => void

export function createDebouncedEffect<T = unknown>(effect: ExtendedReactEffect<T>): UseDebouncedEffect<T> {
const useUpdateEffect = createUpdateEffect(useEffect)

return (callback: EffectCallback, deps?: DependencyList, options?: DebounceOptions, ...args: T[]): void => {
Expand Down
4 changes: 3 additions & 1 deletion packages/react-use/src/utils/create-effect/deep-compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { deepEqual } from '../equal'
import type { DependencyList, EffectCallback } from 'react'
import type { ExtendedReactEffect } from '../basic'

export function createDeepCompareEffect<T = unknown>(effect: ExtendedReactEffect<T>) {
export type UseDeepCompareEffect<T> = (callback: EffectCallback, deps?: DependencyList, ...args: T[]) => void

export function createDeepCompareEffect<T = unknown>(effect: ExtendedReactEffect<T>): UseDeepCompareEffect<T> {
return (callback: EffectCallback, deps?: DependencyList, ...args: T[]): void => {
const updateTriggerRef = useRef({})
const previousDepsRef = useRef(deps)
Expand Down
6 changes: 4 additions & 2 deletions packages/react-use/src/utils/create-effect/once.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { EffectCallback } from 'react'
import type { ExtendedReactEffect } from '../basic'

export function createEffectOnce<T = unknown>(effect: ExtendedReactEffect<T>) {
return (callback: EffectCallback, ...args: T[]) => {
export type UseEffectOnce<T> = (callback: EffectCallback, ...args: T[]) => void

export function createEffectOnce<T = unknown>(effect: ExtendedReactEffect<T>): UseEffectOnce<T> {
return (callback: EffectCallback, ...args: T[]): void => {
effect(callback, [], ...args)
}
}
9 changes: 8 additions & 1 deletion packages/react-use/src/utils/create-effect/throttled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import type { DependencyList, EffectCallback } from 'react'
import type { ExtendedReactEffect } from '../basic'
import type { ThrottleOptions } from '../throttle'

export function createThrottledEffect<T = unknown>(effect: ExtendedReactEffect<T>) {
export type UseThrottledEffect<T = unknown> = (
callback: EffectCallback,
deps?: DependencyList,
options?: ThrottleOptions,
...args: T[]
) => void

export function createThrottledEffect<T = unknown>(effect: ExtendedReactEffect<T>): UseThrottledEffect<T> {
const useUpdateEffect = createUpdateEffect(useEffect)

return (callback: EffectCallback, deps?: DependencyList, options?: ThrottleOptions, ...args: T[]): void => {
Expand Down
4 changes: 3 additions & 1 deletion packages/react-use/src/utils/create-effect/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { createEffectOnce } from './once'
import type { DependencyList } from 'react'
import type { ExtendedReactEffect } from '../basic'

export function createUpdateEffect<T = unknown>(effect: ExtendedReactEffect<T>) {
export type UseUpdateEffect<T = unknown> = (callback: () => void, deps?: DependencyList, ...args: T[]) => void

export function createUpdateEffect<T = unknown>(effect: ExtendedReactEffect<T>): UseUpdateEffect<T> {
const effectOnce = createEffectOnce<T>(effect)

return (callback: () => void, deps?: DependencyList, ...args: T[]): void => {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-use/src/utils/equal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hasOwn } from './basic'
import { reactFastCompare } from './react-fast-compare'

export function shallowEqual<T>(objA: T, objB: T) {
export function shallowEqual<T>(objA: T, objB: T): boolean {
if (Object.is(objA, objB)) {
return true
}
Expand All @@ -26,6 +26,6 @@ export function shallowEqual<T>(objA: T, objB: T) {
return true
}

export function deepEqual<T>(objA: T, objB: T) {
export function deepEqual<T>(objA: T, objB: T): boolean {
return reactFastCompare(objA, objB)
}
2 changes: 1 addition & 1 deletion packages/react-use/src/utils/react-fast-compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const hasArrayBuffer = typeof ArrayBuffer === 'function' && !!ArrayBuffer.isView
* @description `react-fast-compare` rewritten using TypeScript
* @from {@link https://github.com/FormidableLabs/react-fast-compare/blob/6f7d8afe02e4480c32f5af16f571367cccd47abc/index.js | react-fast-compare - GitHub}
*/
export function reactFastCompare(objA: unknown, objB: unknown) {
export function reactFastCompare(objA: unknown, objB: unknown) : boolean {
try {
return equal(objA, objB)
} catch (error: any) {
Expand Down
Loading

0 comments on commit 9a07cf7

Please sign in to comment.