From 230a6f5f96fb91bf228c5c4a1b5387a648eb5ae5 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Sun, 17 Mar 2024 16:57:44 +0100 Subject: [PATCH] align state updater type --- hooks/src/index.d.ts | 12 ++++++++---- hooks/src/index.js | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/hooks/src/index.d.ts b/hooks/src/index.d.ts index 561f034943..3ec8999ca6 100644 --- a/hooks/src/index.d.ts +++ b/hooks/src/index.d.ts @@ -2,20 +2,24 @@ import { ErrorInfo, PreactContext, Ref as PreactRef } from '../..'; type Inputs = ReadonlyArray; -export type StateUpdater = (value: S | ((prevState: S) => S)) => void; +export type Dispatch = (value: A) => void; +export type StateUpdater = S | ((prevState: S) => S); + /** * Returns a stateful value, and a function to update it. * @param initialState The initial value (or a function that returns the initial value) */ -export function useState(initialState: S | (() => S)): [S, StateUpdater]; +export function useState( + initialState: S | (() => S) +): [S, Dispatch>]; export function useState(): [ S | undefined, - StateUpdater + Dispatch> ]; export type Reducer = (prevState: S, action: A) => S; -export type Dispatch = (action: A) => void; + /** * An alternative to `useState`. * diff --git a/hooks/src/index.js b/hooks/src/index.js index f2a0c12922..094410d7a3 100644 --- a/hooks/src/index.js +++ b/hooks/src/index.js @@ -167,7 +167,7 @@ function getHookState(index, type) { /** * @template {unknown} S - * @param {import('./index').StateUpdater} [initialState] + * @param {import('./index').Dispatch>} [initialState] * @returns {[S, (state: S) => void]} */ export function useState(initialState) { @@ -179,7 +179,7 @@ export function useState(initialState) { * @template {unknown} S * @template {unknown} A * @param {import('./index').Reducer} reducer - * @param {import('./index').StateUpdater} initialState + * @param {import('./index').Dispatch>} initialState * @param {(initialState: any) => void} [init] * @returns {[ S, (state: S) => void ]} */