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

fix: swr infers incorrect data type for default SWRConfig generic type #2506

Merged
merged 4 commits into from
Mar 14, 2023
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
28 changes: 16 additions & 12 deletions _internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,23 +232,27 @@ export interface SWRHook {
SWRKey extends Key = StrictKey,
SWROptions extends
| SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>>
| undefined = SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>>
| undefined =
| SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>>
| undefined
>(
key: SWRKey,
config: SWROptions | undefined
): SWRResponse<Data, Error, Required<SWROptions>>
config: SWROptions
): SWRResponse<Data, Error, SWROptions>
<
Data = any,
Error = any,
SWRKey extends Key = StrictKey,
SWROptions extends
| SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>>
| undefined = SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>>
| undefined =
| SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>>
| undefined
>(
key: SWRKey,
fetcher: Fetcher<Data, SWRKey> | null,
config: SWROptions | undefined
): SWRResponse<Data, Error, Required<SWROptions>>
config: SWROptions
): SWRResponse<Data, Error, SWROptions>
<Data = any, Error = any>(key: Key): SWRResponse<Data, Error>
<Data = any, Error = any>(
key: Key,
Expand All @@ -259,22 +263,22 @@ export interface SWRHook {
Error = any,
SWROptions extends
| SWRConfiguration<Data, Error, BareFetcher<Data>>
| undefined = SWRConfiguration<Data, Error, BareFetcher<Data>>
| undefined = SWRConfiguration<Data, Error, BareFetcher<Data>> | undefined
>(
key: Key,
config: SWROptions | undefined
): SWRResponse<Data, Error, Required<SWROptions>>
config: SWROptions
): SWRResponse<Data, Error, SWROptions>
<
Data = any,
Error = any,
SWROptions extends
| SWRConfiguration<Data, Error, BareFetcher<Data>>
| undefined = SWRConfiguration<Data, Error, BareFetcher<Data>>
| undefined = SWRConfiguration<Data, Error, BareFetcher<Data>> | undefined
>(
key: Key,
fetcher: BareFetcher<Data> | null,
config: SWROptions | undefined
): SWRResponse<Data, Error, Required<SWROptions>>
config: SWROptions
): SWRResponse<Data, Error, SWROptions>
}

// Middleware guarantees that a SWRHook receives a key, fetcher, and config as the argument
Expand Down
65 changes: 19 additions & 46 deletions test/type/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import React from 'react'
import type { Cache, SWRResponse } from 'swr'
import useSWR, { useSWRConfig, SWRConfig } from 'swr'
import { expectType } from './utils'
import type {
BareFetcher,
FullConfiguration,
PublicConfiguration
} from 'swr/_internal'
import type { FullConfiguration, SWRConfiguration } from 'swr/_internal'
import type { Equal } from '@type-challenges/utils'

export function testCache() {
Expand Down Expand Up @@ -69,33 +65,22 @@ export function testSuspense() {
})
expectType<any>(data2)

// Generics
// If a generic is explicitly passed we will lose type inference for the swr config
// because of partial inference limitations in typescript.
// https://github.com/microsoft/TypeScript/issues/26242
const { data: data3 } = useSWR<string>(
'/api',
(k: string) => Promise.resolve(k),
{ suspense: true }
)
expectType<string>(data3)
expectType<string | undefined>(data3)

// Generics(default fetcher)
const { data: data4 } = useSWR<string>('/api', { suspense: true })
expectType<string>(data4)

// Generics(SWRKey)
const { data: data5 } = useSWR<string, any, '/api'>(
const { data: data4 } = useSWR<string, any, { suspense: true }>(
'/api',
(k: string) => Promise.resolve(k),
{
suspense: true
}
{ suspense: true }
)
expectType<string>(data5)

// Generics(SWRKey, default fetcher)
const { data: data6 } = useSWR<string, any, '/api'>('/api', {
suspense: true
})
expectType<string>(data6)
expectType<string>(data4)
}

export function testFallbackData() {
Expand Down Expand Up @@ -129,38 +114,26 @@ export function testFallbackData() {
const { data: data4 } = useSWR('/api', { fallbackData: 'fallback' })
expectType<any>(data4)

// Generics
// If a generic is explicitly passed we will lose type inference for the swr config
// because of partial inference limitations in typescript.
// https://github.com/microsoft/TypeScript/issues/26242
const { data: data5 } = useSWR<string>(
'/api',
(k: string) => Promise.resolve(k),
{ fallbackData: 'fallback' }
)
expectType<string>(data5)
expectType<string | undefined>(data5)

// Generics(default fetcher)
const { data: data6 } = useSWR<string>('/api', { fallbackData: 'fallback' })
expectType<string>(data6)

// Generics(SWRKey)
const { data: data7 } = useSWR<string, any, '/api'>(
const { data: data6 } = useSWR<string, any, { fallbackData: 'fallback' }>(
'/api',
(k: string) => Promise.resolve(k),
{ fallbackData: 'fallback' }
)
expectType<string>(data7)

// Generics(SWRKey, default fetcher)
const { data: data8 } = useSWR<string, any, '/api'>('/api', {
fallbackData: 'fallback'
})
expectType<string>(data8)
expectType<string>(data6)
}
export function testUndefined() {
// Undefined can be passed to config.
expectType<
Equal<
Parameters<typeof useSWR<string, any>>['2'],
Partial<PublicConfiguration<string, any, BareFetcher<string>>> | undefined
>
>(true)

export function testConfigAsSWRConfiguration() {
const fetcher = (k: string) => Promise.resolve({ value: k })
const { data } = useSWR('/api', fetcher, {} as SWRConfiguration)
expectType<Equal<typeof data, { value: string } | undefined>>(true)
}