vercel / swr-site

The official website for SWR.
https://swr.vercel.app
Apache License 2.0
481 stars 351 forks source link

Types for SWRConfig mismatch the documentation #561

Closed samuelhulla closed 7 months ago

samuelhulla commented 7 months ago

The properties in the react-native section seem no longer compatible with the SWRConfiguration type.

In the docs you have the example of

<SWRConfig
  value={{
    provider: () => new Map(),
    isOnline() {
      /* Customize the network state detector */
      return true
    },
    isVisible() {
      /* Customize the visibility state detector */
      return true
    },
    initFocus(callback) {
      /* Register the listener with your state provider */
    },
    initReconnect(callback) {
      /* Register the listener with your state provider */
    }
  }}
>

This however misaligns with the exported type of SWRConfiguration as the following properties are missing

Upon further inspection of the exported index.d.ts, the missing properties come from type Config which is however not exported.

type Config = SWRConfiguration & Partial<ProviderConfiguration> & {
    provider?: (cache: Readonly<Cache>) => Cache;
};

where ProviderConfiguration is defined as

type ProviderConfiguration = {
    initFocus: (callback: () => void) => (() => void) | void;
    initReconnect: (callback: () => void) => (() => void) | void;
};

Unless there's an internal reason for not exposing these methods (which seems contradictory with the react-native documentation) the exported type for SWRConfig should be Config instead

Furthermore when importing the component SWRConfig it just defaults to :any, seems a resolution issue with directly importing react over just importing it's types in the SWRConfig definition. import ReactExports from 'react'

samuelhulla commented 7 months ago

Sorry, didn't realize when clicking on posting issue referral actually posts on the specific docs repo. Closing this issue, adding issue to SWR core repo + PR fix.