From 9a7c76870e4bdc806e44de06e86abe3c2e40fb56 Mon Sep 17 00:00:00 2001 From: Yixuan Xu Date: Wed, 6 Apr 2022 21:44:10 +0800 Subject: [PATCH] type: make type inference simpler (#1908) --- infinite/types.ts | 4 +--- src/types.ts | 6 +----- test/type/fetcher.ts | 8 ++++---- test/type/option-fetcher.ts | 10 +++++----- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/infinite/types.ts b/infinite/types.ts index 7cfd0d3a6..b714c1a17 100644 --- a/infinite/types.ts +++ b/infinite/types.ts @@ -6,9 +6,7 @@ export type SWRInfiniteFetcher< Data = any, KeyLoader extends SWRInfiniteKeyLoader = SWRInfiniteKeyLoader > = KeyLoader extends (...args: any[]) => any - ? ReturnType extends readonly [...infer T] - ? (args: T) => FetcherResponse - : ReturnType extends infer T | null | false | undefined + ? ReturnType extends infer T | null | false | undefined ? (args: T) => FetcherResponse : never : never diff --git a/src/types.ts b/src/types.ts index 4c42b0040..e76ddf8d3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -7,11 +7,7 @@ export type BareFetcher = ( export type Fetcher< Data = unknown, SWRKey extends Key = Key -> = SWRKey extends () => readonly [...infer Args] - ? (args: Args) => FetcherResponse - : SWRKey extends readonly [...infer Args] - ? (args: Args) => FetcherResponse - : SWRKey extends () => infer Arg | null | undefined | false +> = SWRKey extends () => infer Arg | null | undefined | false ? (args: Arg) => FetcherResponse : SWRKey extends null | undefined | false ? never diff --git a/test/type/fetcher.ts b/test/type/fetcher.ts index e1b1ebb4a..c439ed28c 100644 --- a/test/type/fetcher.ts +++ b/test/type/fetcher.ts @@ -78,7 +78,7 @@ export function useTuple() { export function useReadonlyTuple() { useSWR([{ a: '1', b: { c: '3' } }, [1231, '888']] as const, keys => { expectType< - [ + readonly [ { readonly a: '1' readonly b: { @@ -94,7 +94,7 @@ export function useReadonlyTuple() { truthy() ? ([{ a: '1', b: { c: '3' } }, [1231, '888']] as const) : null, keys => { expectType< - [ + readonly [ { readonly a: '1' readonly b: { @@ -111,7 +111,7 @@ export function useReadonlyTuple() { truthy() ? ([{ a: '1', b: { c: '3' } }, [1231, '888']] as const) : false, keys => { expectType< - [ + readonly [ { readonly a: '1' readonly b: { @@ -300,7 +300,7 @@ export function useReturnReadonlyTuple() { () => [{ a: '1', b: { c: '3' } }, [1231, '888']] as const, keys => { expectType< - [ + readonly [ { readonly a: '1' readonly b: { diff --git a/test/type/option-fetcher.ts b/test/type/option-fetcher.ts index 4e34a19ac..1b9bd0b8b 100644 --- a/test/type/option-fetcher.ts +++ b/test/type/option-fetcher.ts @@ -80,7 +80,7 @@ export function useReadonlyTuple() { useSWR([{ a: '1', b: { c: '3' } }, [1231, '888']] as const, { fetcher: keys => { expectType< - [ + readonly [ { readonly a: '1' readonly b: { @@ -98,7 +98,7 @@ export function useReadonlyTuple() { { fetcher: keys => { expectType< - [ + readonly [ { readonly a: '1' readonly b: { @@ -117,7 +117,7 @@ export function useReadonlyTuple() { { fetcher: keys => { expectType< - [ + readonly [ { readonly a: '1' readonly b: { @@ -318,7 +318,7 @@ export function useReturnReadonlyTuple() { useSWR(() => [{ a: '1', b: { c: '3' } }, [1231, '888']] as const, { fetcher: keys => { expectType< - [ + readonly [ { readonly a: '1' readonly b: { @@ -376,7 +376,7 @@ export function useReturnReadonlyTuple() { useSWRInfinite(() => [{ a: '1', b: { c: '3' } }, [1231, '888']] as const, { fetcher: keys => { expectType< - [ + readonly [ { readonly a: '1' readonly b: {