Infinite Loading Types

Purpose

Infinite Loading Types define the TypeScript interfaces and type aliases that enable strong typing and configuration of the infinite loading feature built upon the core SWR hook. This subtopic addresses the need to precisely type the parameters, fetchers, responses, and mutation options specific to paginated and infinite data fetching scenarios. By establishing these types, it ensures type safety, IntelliSense support, and clear API contracts for developers using or extending the infinite loading functionality.

Functionality

This subtopic specifies types for several key aspects:

These types collectively enable precise typings for the infinite loading sub-feature, ensuring consistent behavior and extensibility.

Integration

This subtopic tightly integrates with the parent topic "Infinite Loading Support" by providing the foundational type contracts that the infinite loading middleware and hooks implement. While the parent topic focuses on the runtime behavior, caching, and data concatenation for paginated requests, this subtopic defines the shape of the configuration, fetchers, and responses that drive those implementations.

It complements other subtopics by:

By providing these types, it allows the "Infinite Hook Implementation" and associated modules to enforce type safety and clarity, improving developer experience and reducing runtime errors.


Code Snippets Illustrating Key Types

Key Loader Type

Defines the function signature used to generate keys per page:

export type SWRInfiniteKeyLoader<Data = any, Args extends Arguments = Arguments> = (
  index: number,
  previousPageData: Data | null
) => Args

Infinite Response Interface

Extends SWR response with pagination controls:

export interface SWRInfiniteResponse<Data = any, Error = any> extends Omit<SWRResponse<Data[], Error>, 'mutate'> {
  size: number
  setSize: (size: number | ((_size: number) => number)) => Promise<Data[] | undefined>
  mutate: SWRInfiniteKeyedMutator<Data[]>
}

Hook Overloads

Typing the infinite loading hook with flexibility:

export interface SWRInfiniteHook {
  <Data = any, Error = any, KeyLoader extends SWRInfiniteKeyLoader = ...>(
    getKey: KeyLoader,
    fetcher?: SWRInfiniteFetcher<Data, KeyLoader> | null,
    config?: SWRInfiniteConfiguration<Data, Error, SWRInfiniteFetcher<Data, KeyLoader>>
  ): SWRInfiniteResponse<Data, Error>
}

Diagram: Infinite Loading Type Relationships

classDiagram
  class SWRInfiniteHook {
    +getKey: SWRInfiniteKeyLoader
    +fetcher?: SWRInfiniteFetcher
    +config?: SWRInfiniteConfiguration
    +returns SWRInfiniteResponse
  }

  class SWRInfiniteKeyLoader {
    +index: number
    +previousPageData: Data | null
    +returns Args
  }

  class SWRInfiniteFetcher {
    +args: Args
    +returns Data | Promise<Data>
  }

  class SWRInfiniteConfiguration {
    +initialSize: number
    +revalidateAll: boolean
    +persistSize: boolean
    +parallel: boolean
    +fetcher: SWRInfiniteFetcher
    +compare: SWRInfiniteCompareFn
  }

  class SWRInfiniteResponse {
    +size: number
    +setSize(size)
    +mutate(data, opts)
  }

  SWRInfiniteHook --> SWRInfiniteKeyLoader : uses
  SWRInfiniteHook --> SWRInfiniteFetcher : optionally uses
  SWRInfiniteHook --> SWRInfiniteConfiguration : optionally uses
  SWRInfiniteHook --> SWRInfiniteResponse : returns
  SWRInfiniteResponse --> SWRInfiniteKeyedMutator : mutate type
  SWRInfiniteConfiguration --> SWRInfiniteFetcher : fetcher type

This diagram visualizes the core types and their interactions that define the infinite loading feature's type system, highlighting how the hook uses key loaders, fetchers, and configuration to return a response with pagination controls.


By clearly defining these types, the infinite loading feature ensures type-safe, predictable, and extensible usage patterns that enhance both developer experience and runtime correctness.