Skip to content

Commit 230a6f5

Browse files
committed
align state updater type
1 parent dc55841 commit 230a6f5

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

hooks/src/index.d.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@ import { ErrorInfo, PreactContext, Ref as PreactRef } from '../..';
22

33
type Inputs = ReadonlyArray<unknown>;
44

5-
export type StateUpdater<S> = (value: S | ((prevState: S) => S)) => void;
5+
export type Dispatch<A> = (value: A) => void;
6+
export type StateUpdater<S> = S | ((prevState: S) => S);
7+
68
/**
79
* Returns a stateful value, and a function to update it.
810
* @param initialState The initial value (or a function that returns the initial value)
911
*/
10-
export function useState<S>(initialState: S | (() => S)): [S, StateUpdater<S>];
12+
export function useState<S>(
13+
initialState: S | (() => S)
14+
): [S, Dispatch<StateUpdater<S>>];
1115

1216
export function useState<S = undefined>(): [
1317
S | undefined,
14-
StateUpdater<S | undefined>
18+
Dispatch<StateUpdater<S | undefined>>
1519
];
1620

1721
export type Reducer<S, A> = (prevState: S, action: A) => S;
18-
export type Dispatch<A> = (action: A) => void;
22+
1923
/**
2024
* An alternative to `useState`.
2125
*

hooks/src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function getHookState(index, type) {
167167

168168
/**
169169
* @template {unknown} S
170-
* @param {import('./index').StateUpdater<S>} [initialState]
170+
* @param {import('./index').Dispatch<import('./index').StateUpdater<S>>} [initialState]
171171
* @returns {[S, (state: S) => void]}
172172
*/
173173
export function useState(initialState) {
@@ -179,7 +179,7 @@ export function useState(initialState) {
179179
* @template {unknown} S
180180
* @template {unknown} A
181181
* @param {import('./index').Reducer<S, A>} reducer
182-
* @param {import('./index').StateUpdater<S>} initialState
182+
* @param {import('./index').Dispatch<import('./index').StateUpdater<S>>} initialState
183183
* @param {(initialState: any) => void} [init]
184184
* @returns {[ S, (state: S) => void ]}
185185
*/

0 commit comments

Comments
 (0)