use-persisted-state icon indicating copy to clipboard operation
use-persisted-state copied to clipboard

typings file for module

Open thomasmost opened this issue 4 years ago • 3 comments

I'm not 100% sure this is correct... in fact I'm only around 60% sure. But I figured I'd open a PR and you could tell me what I'm getting wrong 🙂

This will help us convert the the SessionsFM AuthProvider to TypeScript

thomasmost avatar May 14 '20 03:05 thomasmost

I believe the file name needs to be index.d.ts for typescript to catch the declarations.

mmtftr avatar Jun 19 '20 20:06 mmtftr

we should probably define the typings for Dispatch e SetStateAction first — might use React.Dispatch and React.SetStateAction

the export should also use the same type on the tuple as the one provided on the createPersistedState call

declare module 'use-persisted-state' {
  function createPersistedState<T>(key: string, storage?: Storage): (initialState: T) => [T, React.Dispatch<React.SetStateAction<T>>]
  export = createPersistedState
}

or directly on the hook call (to closer to useState, hook for example):

declare module 'use-persisted-state' {
  function createPersistedState(key: string, storage?: Storage): <T>(initialState: T) => [T, React.Dispatch<React.SetStateAction<T>>]
  export = createPersistedState
}

vitordino avatar Dec 04 '20 21:12 vitordino

i'm using a version based on useState:

declare module "use-persisted-state" {
  function createPersistedState<S>(
    key: string,
    storage?: Storage
  ): (
    initialState: S | (() => S)
  ) => [S, React.Dispatch<React.SetStateAction<S>>];

  function createPersistedState<S = undefined>(
    key: string,
    storage?: Storage
  ): (
    initialState: S | undefined
  ) => [S | undefined, React.Dispatch<React.SetStateAction<S>>];

  export = createPersistedState;
}

gullitmiranda avatar Jan 06 '21 16:01 gullitmiranda