Skip to content

Commit

Permalink
enhance: Improve args type matching for hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Apr 25, 2024
1 parent b5c155c commit fc13ecb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/react/src/NoInfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type NI<T> = NoInfer<T>;
3 changes: 0 additions & 3 deletions packages/react/src/hooks/__tests__/useSuspense.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,6 @@ describe('useSuspense()', () => {
sortBy: 'votes',
});

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore TODO: if you put a string that doesn't match it will complain
// but it somehow grabs the args as what it extends, making it include null
comments.map(comment => comment.title);
}
};
Expand Down
31 changes: 28 additions & 3 deletions packages/react/src/hooks/useLive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import {
Denormalize,
Schema,
FetchFunction,
ResolveType,
DenormalizeNullable,
} from '@data-client/core';

import { SuspenseReturn } from './types.js';
import useSubscription from './useSubscription.js';
import useSuspense from './useSuspense.js';
import { NI } from '../NoInfer.js';

/**
* Ensure an endpoint is available. Keeps it fresh once it is.
Expand All @@ -24,8 +26,31 @@ export default function useLive<
Schema | undefined,
undefined | false
>,
Args extends readonly [...Parameters<E>] | readonly [null],
>(endpoint: E, ...args: Args): SuspenseReturn<E, Args> {
>(
endpoint: E,
...args: readonly [...Parameters<NI<E>>]
): E['schema'] extends undefined | null ? ResolveType<E>
: Denormalize<E['schema']>;

export default function useLive<
E extends EndpointInterface<
FetchFunction,
Schema | undefined,
undefined | false
>,
>(
endpoint: E,
...args: readonly [...Parameters<NI<E>>] | readonly [null]
): E['schema'] extends undefined | null ? ResolveType<E> | undefined
: DenormalizeNullable<E['schema']>;

export default function useLive<
E extends EndpointInterface<
FetchFunction,
Schema | undefined,
undefined | false
>,
>(endpoint: E, ...args: readonly [...Parameters<E>] | readonly [null]): any {
useSubscription(endpoint, ...args);
return useSuspense(endpoint, ...args);
}
31 changes: 28 additions & 3 deletions packages/react/src/hooks/useSuspense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import {
Denormalize,
Schema,
FetchFunction,
DenormalizeNullable,
ResolveType,
} from '@data-client/core';
import { useMemo } from 'react';

import { SuspenseReturn } from './types.js';
import useCacheState from './useCacheState.js';
import useController from '../hooks/useController.js';
import { NI } from '../NoInfer.js';

/**
* Ensure an endpoint is available.
Expand All @@ -27,8 +29,31 @@ export default function useSuspense<
Schema | undefined,
undefined | false
>,
Args extends readonly [...Parameters<E>] | readonly [null],
>(endpoint: E, ...args: Args): SuspenseReturn<E, Args> {
>(
endpoint: E,
...args: readonly [...Parameters<NI<E>>]
): E['schema'] extends undefined | null ? ResolveType<E>
: Denormalize<E['schema']>;

export default function useSuspense<
E extends EndpointInterface<
FetchFunction,
Schema | undefined,
undefined | false
>,
>(
endpoint: E,
...args: readonly [...Parameters<NI<E>>] | readonly [null]
): E['schema'] extends undefined | null ? ResolveType<E> | undefined
: DenormalizeNullable<E['schema']>;

export default function useSuspense<
E extends EndpointInterface<
FetchFunction,
Schema | undefined,
undefined | false
>,
>(endpoint: E, ...args: readonly [...Parameters<E>] | readonly [null]): any {
const state = useCacheState();
const controller = useController();

Expand Down

0 comments on commit fc13ecb

Please sign in to comment.