Skip to content

Commit

Permalink
type: make type inference simpler (#1908)
Browse files Browse the repository at this point in the history
  • Loading branch information
promer94 committed Apr 6, 2022
1 parent d4f7923 commit 9a7c768
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
4 changes: 1 addition & 3 deletions infinite/types.ts
Expand Up @@ -6,9 +6,7 @@ export type SWRInfiniteFetcher<
Data = any,
KeyLoader extends SWRInfiniteKeyLoader = SWRInfiniteKeyLoader
> = KeyLoader extends (...args: any[]) => any
? ReturnType<KeyLoader> extends readonly [...infer T]
? (args: T) => FetcherResponse<Data>
: ReturnType<KeyLoader> extends infer T | null | false | undefined
? ReturnType<KeyLoader> extends infer T | null | false | undefined
? (args: T) => FetcherResponse<Data>
: never
: never
Expand Down
6 changes: 1 addition & 5 deletions src/types.ts
Expand Up @@ -7,11 +7,7 @@ export type BareFetcher<Data = unknown> = (
export type Fetcher<
Data = unknown,
SWRKey extends Key = Key
> = SWRKey extends () => readonly [...infer Args]
? (args: Args) => FetcherResponse<Data>
: SWRKey extends readonly [...infer Args]
? (args: Args) => FetcherResponse<Data>
: SWRKey extends () => infer Arg | null | undefined | false
> = SWRKey extends () => infer Arg | null | undefined | false
? (args: Arg) => FetcherResponse<Data>
: SWRKey extends null | undefined | false
? never
Expand Down
8 changes: 4 additions & 4 deletions test/type/fetcher.ts
Expand Up @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand Down Expand Up @@ -300,7 +300,7 @@ export function useReturnReadonlyTuple() {
() => [{ a: '1', b: { c: '3' } }, [1231, '888']] as const,
keys => {
expectType<
[
readonly [
{
readonly a: '1'
readonly b: {
Expand Down
10 changes: 5 additions & 5 deletions test/type/option-fetcher.ts
Expand Up @@ -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: {
Expand All @@ -98,7 +98,7 @@ export function useReadonlyTuple() {
{
fetcher: keys => {
expectType<
[
readonly [
{
readonly a: '1'
readonly b: {
Expand All @@ -117,7 +117,7 @@ export function useReadonlyTuple() {
{
fetcher: keys => {
expectType<
[
readonly [
{
readonly a: '1'
readonly b: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit 9a7c768

Please sign in to comment.