import { useReducerImpl } from "./useReducer"; export namespace useState { export type StateUpdater = S | ((prev: S) => S); } const stateReducer = ( state: S | undefined, action: useState.StateUpdater, ): S | undefined => typeof action === "function" ? (action as (prev: S | undefined) => S)(state) : action; const stateInit = (initial: S | (() => S) | undefined): S | undefined => initial === undefined ? undefined : typeof initial === "function" ? (initial as () => S)() : initial; export function useState(): [ S | undefined, (updater: useState.StateUpdater) => void, ]; export function useState( initial: S | (() => S), ): [S, (updater: useState.StateUpdater) => void]; export function useState( initial?: S | (() => S), ): [S | undefined, (updater: useState.StateUpdater) => void] { return useReducerImpl< S | undefined, useState.StateUpdater, S | (() => S) | undefined >(stateReducer, initial, stateInit, true); }