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

typings file for module #32

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open

Conversation

thomasmost
Copy link

@thomasmost thomasmost commented May 14, 2020

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

Comment on lines +1 to +2
declare module 'use-persisted-state' {
function createPersistedState<T>(key: string, storage: Storage): (initialState: T) => [S, Dispatch<SetStateAction<S>>];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to make storage optional via storage?: Storage, since it has a default

@@ -0,0 +1,4 @@
declare module 'use-persisted-state' {
function createPersistedState<T>(key: string, storage: Storage): (initialState: T) => [S, Dispatch<SetStateAction<S>>];
export = createPersistedState;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is the default export, I had to use export default createPersistedState. Without that, it would work fine, but TS was complaining about synthetic default imports

@mmtftr
Copy link

mmtftr commented Jun 19, 2020

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

@vitordino
Copy link
Contributor

vitordino commented Dec 4, 2020

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
}

@gullitmiranda
Copy link

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;
}

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.

None yet

5 participants