Skip to content

fix(types): allow null as fetcher in PublicConfiguration - #4290

Draft
okxint wants to merge 1 commit into
vercel:mainfrom
okxint:fix/null-fetcher-type
Draft

fix(types): allow null as fetcher in PublicConfiguration#4290
okxint wants to merge 1 commit into
vercel:mainfrom
okxint:fix/null-fetcher-type

Conversation

@okxint

@okxint okxint commented Jul 4, 2026

Copy link
Copy Markdown

What

Adds | null to PublicConfiguration.fetcher so passing null as a fetcher via the config object is accepted by TypeScript.

Why

Passing null as fetcher is a valid runtime pattern to disable fetching for a key. The positional overloads on SWRHook already typed fetcher as Fetcher | null, so useSWR("key", null) worked fine. But PublicConfiguration.fetcher was typed as just Fn, causing type errors for the config-object pattern:

useSWR("key", { fetcher: null })          // ❌ TS error before fix
const cfg: SWRConfiguration = { fetcher: null }  // ❌ TS error before fix
useSWR("key", null)                        // ✅ already worked

Fix

- fetcher?: Fn
+ fetcher?: Fn | null

Also adds a type test covering both patterns to prevent regression.

All 365 unit tests pass. Full tsc --noEmit passes.

Fixes #2888

SWRConfiguration.fetcher previously typed as Fn | undefined, rejecting null.
Passing null is valid at runtime and disables fetching, but TypeScript would
error on useSWR("key", { fetcher: null }) and explicit SWRConfiguration
assignments. Adds | null to PublicConfiguration.fetcher to match the
existing null support in SWRHook positional overloads.

Fixes vercel#2888
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type error when overriding global fetcher function with null

1 participant