Skip to content

Commit b583fad

Browse files
committed
Add return types to functions
1 parent 85e3f71 commit b583fad

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

hooks/src/index.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ options.diffed = vnode => {
8585
previousComponent = currentComponent = null;
8686
};
8787

88-
/** @type {(vnode: import('./internal').VNode) => void} */
88+
// TODO: Improve typing of commitQueue parameter
89+
/** @type {(vnode: import('./internal').VNode, commitQueue: any) => void} */
8990
options._commit = (vnode, commitQueue) => {
9091
commitQueue.some(component => {
9192
try {
@@ -158,6 +159,7 @@ function getHookState(index, type) {
158159
/**
159160
* @template {unknown} S
160161
* @param {import('./index').StateUpdater<S>} [initialState]
162+
* @returns {[S, (state: S) => void]}
161163
*/
162164
export function useState(initialState) {
163165
currentHook = 1;
@@ -230,8 +232,7 @@ export function useReducer(reducer, initialState, init) {
230232
function updateHookState(p, s, c) {
231233
if (!hookState._component.__hooks) return true;
232234

233-
/**
234-
* @type {(x: import('./internal').HookState) => x is import('./internal').ReducerHookState} */
235+
/** @type {(x: import('./internal').HookState) => x is import('./internal').ReducerHookState} */
235236
const isStateHook = x => !!x._component;
236237
const stateHooks =
237238
hookState._component.__hooks._list.filter(isStateHook);
@@ -273,6 +274,7 @@ export function useReducer(reducer, initialState, init) {
273274
/**
274275
* @param {import('./internal').Effect} callback
275276
* @param {unknown[]} args
277+
* @returns {void}
276278
*/
277279
export function useEffect(callback, args) {
278280
/** @type {import('./internal').EffectHookState} */
@@ -288,6 +290,7 @@ export function useEffect(callback, args) {
288290
/**
289291
* @param {import('./internal').Effect} callback
290292
* @param {unknown[]} args
293+
* @returns {void}
291294
*/
292295
export function useLayoutEffect(callback, args) {
293296
/** @type {import('./internal').EffectHookState} */
@@ -310,6 +313,7 @@ export function useRef(initialValue) {
310313
* @param {object} ref
311314
* @param {() => object} createHandle
312315
* @param {unknown[]} args
316+
* @returns {void}
313317
*/
314318
export function useImperativeHandle(ref, createHandle, args) {
315319
currentHook = 6;
@@ -328,11 +332,13 @@ export function useImperativeHandle(ref, createHandle, args) {
328332
}
329333

330334
/**
331-
* @param {() => unknown} factory
335+
* @template {unknown} T
336+
* @param {() => T} factory
332337
* @param {unknown[]} args
338+
* @returns {T}
333339
*/
334340
export function useMemo(factory, args) {
335-
/** @type {import('./internal').MemoHookState} */
341+
/** @type {import('./internal').MemoHookState<T>} */
336342
const state = getHookState(currentIndex++, 7);
337343
if (argsChanged(state._args, args)) {
338344
state._pendingValue = factory();
@@ -347,6 +353,7 @@ export function useMemo(factory, args) {
347353
/**
348354
* @param {() => void} callback
349355
* @param {unknown[]} args
356+
* @returns {() => void}
350357
*/
351358
export function useCallback(callback, args) {
352359
currentHook = 8;
@@ -390,6 +397,7 @@ export function useDebugValue(value, formatter) {
390397

391398
/**
392399
* @param {(error: unknown, errorInfo: import('preact').ErrorInfo) => void} cb
400+
* @returns {[unknown, () => void]}
393401
*/
394402
export function useErrorBoundary(cb) {
395403
/** @type {import('./internal').ErrorBoundaryHookState} */
@@ -479,6 +487,7 @@ function afterNextFrame(callback) {
479487
/**
480488
* Schedule afterPaintEffects flush after the browser paints
481489
* @param {number} newQueueLength
490+
* @returns {void}
482491
*/
483492
function afterPaint(newQueueLength) {
484493
if (newQueueLength === 1 || prevRaf !== options.requestAnimationFrame) {
@@ -489,6 +498,7 @@ function afterPaint(newQueueLength) {
489498

490499
/**
491500
* @param {import('./internal').HookState} hook
501+
* @returns {void}
492502
*/
493503
function invokeCleanup(hook) {
494504
// A hook cleanup can introduce a call to render which creates a new root, this will call options.vnode
@@ -506,6 +516,7 @@ function invokeCleanup(hook) {
506516
/**
507517
* Invoke a Hook's effect
508518
* @param {import('./internal').EffectHookState} hook
519+
* @returns {void}
509520
*/
510521
function invokeEffect(hook) {
511522
// A hook call can introduce a call to render which creates a new root, this will call options.vnode
@@ -518,6 +529,7 @@ function invokeEffect(hook) {
518529
/**
519530
* @param {unknown[]} oldArgs
520531
* @param {unknown[]} newArgs
532+
* @returns {boolean}
521533
*/
522534
function argsChanged(oldArgs, newArgs) {
523535
return (

hooks/src/internal.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ export interface EffectHookState extends BaseHookState {
6464
_cleanup?: Cleanup | void;
6565
}
6666

67-
export interface MemoHookState extends BaseHookState {
68-
_value?: unknown;
69-
_pendingValue?: unknown;
67+
export interface MemoHookState<T = unknown> extends BaseHookState {
68+
_value?: T;
69+
_pendingValue?: T;
7070
_args?: unknown[];
7171
_pendingArgs?: unknown[];
72-
_factory?: () => unknown;
72+
_factory?: () => T;
7373
}
7474

7575
export interface ReducerHookState<S = unknown, A = unknown>

0 commit comments

Comments
 (0)