diff --git a/libs/horizon/src/renderer/hooks/BaseHook.ts b/libs/horizon/src/renderer/hooks/BaseHook.ts index 0109bf62..1cac614f 100644 --- a/libs/horizon/src/renderer/hooks/BaseHook.ts +++ b/libs/horizon/src/renderer/hooks/BaseHook.ts @@ -34,7 +34,7 @@ export function setCurrentHook(hook: Hook | null) { currentHook = hook; } -export function throwNotInFuncError() { +export function throwNotInFuncError(): never { throw Error('Hooks should be used inside function component.'); } diff --git a/libs/horizon/src/renderer/hooks/HookExternal.ts b/libs/horizon/src/renderer/hooks/HookExternal.ts index 60adc0bd..ddc9c8a2 100644 --- a/libs/horizon/src/renderer/hooks/HookExternal.ts +++ b/libs/horizon/src/renderer/hooks/HookExternal.ts @@ -27,22 +27,25 @@ import { getProcessingVNode } from '../GlobalVar'; import { Ref, Trigger } from './HookType'; type BasicStateAction = ((S) => S) | S; -type Dispatch = (A) => void; +type Dispatch = (value: A) => void; export function useContext(Context: ContextType): T { const processingVNode = getProcessingVNode(); return getNewContext(processingVNode!, Context, true); } - -export function useState(initialState: (() => S) | S): [S, Dispatch>] { +export function useState(): [S | undefined, Dispatch>] +export function useState(initialState: (() => S) | S): [S, Dispatch>] +export function useState(initialState?: (() => S) | S): [S, Dispatch>] { return useStateImpl(initialState); } -export function useReducer(reducer: (S, A) => S, initialArg: I, init?: (I) => S): [S, Trigger] | void { +export function useReducer(reducer: (S, A) => S, initialArg: I, init?: (I) => S): [S, Trigger] { return useReducerImpl(reducer, initialArg, init); } -export function useRef(initialValue: T): Ref { +export function useRef(): Ref +export function useRef(initialValue: T): Ref +export function useRef(initialValue?: T): Ref { return useRefImpl(initialValue); } diff --git a/libs/horizon/src/renderer/hooks/HookType.ts b/libs/horizon/src/renderer/hooks/HookType.ts index fddf5223..a682b9a4 100644 --- a/libs/horizon/src/renderer/hooks/HookType.ts +++ b/libs/horizon/src/renderer/hooks/HookType.ts @@ -57,4 +57,4 @@ export type Ref = { current: V; }; -export type Trigger = (A) => void; +export type Trigger = (state: A) => void; diff --git a/libs/horizon/src/renderer/hooks/UseRefHook.ts b/libs/horizon/src/renderer/hooks/UseRefHook.ts index a9e597fb..b4becc2b 100644 --- a/libs/horizon/src/renderer/hooks/UseRefHook.ts +++ b/libs/horizon/src/renderer/hooks/UseRefHook.ts @@ -17,7 +17,7 @@ import { createHook, getCurrentHook, throwNotInFuncError } from './BaseHook'; import { getHookStage, HookStage } from './HookStage'; import type { Ref } from './HookType'; -export function useRefImpl(value: V): Ref { +export function useRefImpl(value?: V): Ref { const stage = getHookStage(); if (stage === null) { throwNotInFuncError(); diff --git a/libs/horizon/src/renderer/hooks/UseStateHook.ts b/libs/horizon/src/renderer/hooks/UseStateHook.ts index e7d10eb0..78135114 100644 --- a/libs/horizon/src/renderer/hooks/UseStateHook.ts +++ b/libs/horizon/src/renderer/hooks/UseStateHook.ts @@ -21,6 +21,6 @@ function defaultReducer(state: S, action: ((S) => S) | S): S { return typeof action === 'function' ? action(state) : action; } -export function useStateImpl(initArg: (() => S) | S): [S, Trigger<((S) => S) | S>] { +export function useStateImpl(initArg?: (() => S) | S): [S, Trigger<((S) => S) | S>] { return useReducerImpl(defaultReducer, initArg, undefined, true); }