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

Argument of type '"cart"' is not assignable to parameter of type 'never' #51

Closed
FutureKode opened this issue Oct 15, 2020 · 5 comments
Closed

Comments

@FutureKode
Copy link

I have a NextJs project with typescript. I'm seeing the above error with useGlobalState and reducer style.

const [cart] = useGlobalState("cart") <-- Argument of type '"cart"' is not assignable to parameter of type 'never'

But I'm not sure what the problem is at it looks the same as the examples. Any ideas?

@dai-shi
Copy link
Owner

dai-shi commented Oct 15, 2020

Hi, I need some more information.

  1. how does createGlobalState look like? does it initialize { cart: ... }?
  2. can you repro the issue without nextjs? a codesandbox repro would be nice.

@FutureKode
Copy link
Author

Hi @dai-shi

This is the relative code:

import { createStore } from "react-hooks-global-state"

interface State {
  cart: Array<ICartItem>
  cookiesNoticeSeen: boolean
}

type Action =
  | { type: "rehydrate"; payload: any }
  | { type: "addItem"; item: ICartItem }
  | { type: "removeItem"; id: string }
  | { type: "setCookiesNoticeSeen" }

const defaultState: State = {
  cookiesNoticeSeen: false,
  cart: [],
}

const initialState: State = defaultState

const saveStateToStorage = ({ getState }: { getState: () => State }) => (
  next: Dispatch<Action>
) => (action: Action) => {
  const returnValue = next(action)
  if (isBrowser()) {
    localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(getState()))
  }
  return returnValue
}

export const { dispatch, useGlobalState } = createStore(
  reducer,
  initialState,
  applyMiddleware(saveStateToStorage)
)

@dai-shi
Copy link
Owner

dai-shi commented Oct 16, 2020

It looks good...
Does removing middleware change things?
Can you repro with codesandbox?

@FutureKode
Copy link
Author

I have found the lines in my code that are causing the issue.

case "rehydrate":   
      console.log('rehydrating', action)
      return action.payload

If I remove this code from my reducer then the errors go away

@FutureKode
Copy link
Author

@dai-shi yep sorry about that, reworked my code and the error is gone 👻

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

No branches or pull requests

2 participants