Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: Infer returntype of useFetcher()'s functions #310

Merged
merged 1 commit into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/rest-hooks/src/react-integration/hooks/useFetcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useContext, useRef, useCallback } from 'react';
import { FetchAction, UpdateFunction, ReceiveTypes } from 'rest-hooks/types';
import {
RECEIVE_DELETE_TYPE,
Expand All @@ -17,6 +16,8 @@ import {
} from 'rest-hooks/resource';
import { DispatchContext } from 'rest-hooks/react-integration/context';

import { useContext, useRef, useCallback } from 'react';

const SHAPE_TYPE_TO_RESPONSE_TYPE: Record<
FetchShape<any, any, any>['type'],
ReceiveTypes
Expand Down Expand Up @@ -46,7 +47,10 @@ export default function useFetcher<
fetchShape: Shape,
throttle = false,
): Shape extends DeleteShape<any, any, any>
? (params: ParamsFromShape<Shape>, body: BodyFromShape<Shape>) => Promise<any>
? (
params: ParamsFromShape<Shape>,
body: BodyFromShape<Shape>,
) => ReturnType<typeof fetchShape['fetch']>
: <
UpdateParams extends OptimisticUpdateParams<
SchemaFromShape<Shape>,
Expand All @@ -56,7 +60,7 @@ export default function useFetcher<
params: ParamsFromShape<Shape>,
body: BodyFromShape<Shape>,
updateParams?: UpdateParams | undefined,
) => Promise<any> {
) => ReturnType<typeof fetchShape['fetch']> {
const dispatch = useContext(DispatchContext);

// we just want the current values when we dispatch, so
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useMemo, useContext } from 'react';
import {
ReadShape,
Denormalize,
Expand All @@ -8,6 +7,8 @@ import {
import { useDenormalized } from 'rest-hooks/state/selectors';
import { StateContext } from 'rest-hooks/react-integration/context';

import { useMemo, useContext } from 'react';

import useRetrieve from './useRetrieve';
import useError from './useError';
import hasUsableData from './hasUsableData';
Expand Down
10 changes: 5 additions & 5 deletions packages/rest-hooks/src/react-integration/hooks/useRetrieve.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { ReadShape, Schema } from 'rest-hooks/resource';
import { ReadShape, ParamsFromShape } from 'rest-hooks/resource';

import { useMemo } from 'react';

import useFetcher from './useFetcher';
import useExpiresAt from './useExpiresAt';

/** Request a resource if it is not in cache. */
export default function useRetrieve<
Params extends Readonly<object>,
S extends Schema
>(fetchShape: ReadShape<S, Params>, params: Params | null) {
export default function useRetrieve<Shape extends ReadShape<any, any>>(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parameter of Shape rather than a part gives us more information. this is used in useFetcher() which infers its return type

fetchShape: Shape,
params: ParamsFromShape<Shape> | null,
) {
const fetch = useFetcher(fetchShape, true);
const expiresAt = useExpiresAt(fetchShape, params);

Expand Down