From 26239d944a3c3268bc389a749328e73bdbefd163 Mon Sep 17 00:00:00 2001 From: YixuanXu Date: Sat, 25 Sep 2021 22:00:26 +0800 Subject: [PATCH] simplify the implementation of fetcher type Author: YixuanXu --- infinite/index.ts | 8 +++----- src/types.ts | 42 ++++++++++++++++-------------------------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/infinite/index.ts b/infinite/index.ts index 9880fec98..112077241 100644 --- a/infinite/index.ts +++ b/infinite/index.ts @@ -252,13 +252,11 @@ export const infinite = ((( export type InfiniteFetcher< Args extends ValueKey = ValueKey, - Data = unknown + Data = any > = Args extends (readonly [...infer K]) ? ((...args: [...K]) => Result) - : Args extends (string | null) - ? Args extends (Record | null) - ? never - : (...args: [string]) => Result + : Args extends null + ? never : Args extends (infer T) ? (...args: [T]) => Result : never diff --git a/src/types.ts b/src/types.ts index 12a54823e..5ebf1e913 100644 --- a/src/types.ts +++ b/src/types.ts @@ -10,34 +10,24 @@ export type Fetcher = */ Args extends (() => readonly [...infer K] | null) ? ((...args: [...K]) => Result) - /** - * [{ foo: string }, { bar: number } ] | null - * - * [{ foo: string }, { bar: number } ] as const | null - */ - : Args extends (readonly [...infer K]) + : /** + * [{ foo: string }, { bar: number } ] | null + * + * [{ foo: string }, { bar: number } ] as const | null + */ + Args extends (readonly [...infer K]) ? ((...args: [...K]) => Result) - /** - * () => string | null - * () => Record | null - */ - : Args extends (() => infer T | null) + : /** + * () => string | null + * () => Record | null + */ + Args extends (() => infer T | null) ? (...args: [T]) => Result - /** - * string | null - */ - : Args extends (string | null) - /** - * when key is Record | null - * use { foo: string, bar: number } | null as example - * - * the fetcher would be - * (arg0: string) => any | (arg0: { foo: string, bar: number }) => any - * so we add this condition to make (arg0: string) => any to be never - */ - ? Args extends (Record | null) - ? never - : (...args: [string]) => Result + : /** + * string | null | Record + */ + Args extends null + ? never : Args extends (infer T) ? (...args: [T]) => Result : never