Skip to content

Commit

Permalink
simplify the implementation of fetcher type
Browse files Browse the repository at this point in the history
Author:    YixuanXu <yixuanxu94@outlook.com>
  • Loading branch information
promer94 committed Sep 25, 2021
1 parent 7ff0020 commit 26239d9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 31 deletions.
8 changes: 3 additions & 5 deletions infinite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,11 @@ export const infinite = ((<Data, Error, Args extends ValueKey>(

export type InfiniteFetcher<
Args extends ValueKey = ValueKey,
Data = unknown
Data = any
> = Args extends (readonly [...infer K])
? ((...args: [...K]) => Result<Data>)
: Args extends (string | null)
? Args extends (Record<any, any> | null)
? never
: (...args: [string]) => Result<Data>
: Args extends null
? never
: Args extends (infer T)
? (...args: [T]) => Result<Data>
: never
Expand Down
42 changes: 16 additions & 26 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,24 @@ export type Fetcher<Data = unknown, Args extends Key = Key> =
*/
Args extends (() => readonly [...infer K] | null)
? ((...args: [...K]) => Result<Data>)
/**
* [{ 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<Data>)
/**
* () => string | null
* () => Record<any, any> | null
*/
: Args extends (() => infer T | null)
: /**
* () => string | null
* () => Record<any, any> | null
*/
Args extends (() => infer T | null)
? (...args: [T]) => Result<Data>
/**
* string | null
*/
: Args extends (string | null)
/**
* when key is Record<any, any> | 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<any, any> | null)
? never
: (...args: [string]) => Result<Data>
: /**
* string | null | Record<any,any>
*/
Args extends null
? never
: Args extends (infer T)
? (...args: [T]) => Result<Data>
: never
Expand Down

0 comments on commit 26239d9

Please sign in to comment.