Skip to content

Commit

Permalink
align state updater type
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Mar 17, 2024
1 parent dc55841 commit 230a6f5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 8 additions & 4 deletions hooks/src/index.d.ts
Expand Up @@ -2,20 +2,24 @@ import { ErrorInfo, PreactContext, Ref as PreactRef } from '../..';

type Inputs = ReadonlyArray<unknown>;

export type StateUpdater<S> = (value: S | ((prevState: S) => S)) => void;
export type Dispatch<A> = (value: A) => void;
export type StateUpdater<S> = 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<S>(initialState: S | (() => S)): [S, StateUpdater<S>];
export function useState<S>(
initialState: S | (() => S)
): [S, Dispatch<StateUpdater<S>>];

export function useState<S = undefined>(): [
S | undefined,
StateUpdater<S | undefined>
Dispatch<StateUpdater<S | undefined>>
];

export type Reducer<S, A> = (prevState: S, action: A) => S;
export type Dispatch<A> = (action: A) => void;

/**
* An alternative to `useState`.
*
Expand Down
4 changes: 2 additions & 2 deletions hooks/src/index.js
Expand Up @@ -167,7 +167,7 @@ function getHookState(index, type) {

/**
* @template {unknown} S
* @param {import('./index').StateUpdater<S>} [initialState]
* @param {import('./index').Dispatch<import('./index').StateUpdater<S>>} [initialState]
* @returns {[S, (state: S) => void]}
*/
export function useState(initialState) {
Expand All @@ -179,7 +179,7 @@ export function useState(initialState) {
* @template {unknown} S
* @template {unknown} A
* @param {import('./index').Reducer<S, A>} reducer
* @param {import('./index').StateUpdater<S>} initialState
* @param {import('./index').Dispatch<import('./index').StateUpdater<S>>} initialState
* @param {(initialState: any) => void} [init]
* @returns {[ S, (state: S) => void ]}
*/
Expand Down

0 comments on commit 230a6f5

Please sign in to comment.