Skip to content

How to use a generic interface as store type? #841

Closed Answered by dai-shi
xypnox asked this question in Q&A
Discussion options

You must be logged in to vote

How about this?
https://codesandbox.io/s/tender-bell-95sko3?file=/src/App.tsx

interface StoreState<Item> {
  items: Item[];
}

const useStoreBase = create<StoreState<any>>((set) => ({
  items: []
}));

export const useStore = <Item, Slice>(
  selector: (state: StoreState<Item>) => Slice
) => useStoreBase(selector);

Replies: 2 comments 11 replies

Comment options

You must be logged in to vote
8 replies
@AndresdoSantos
Comment options

@dai-shi
Comment options

@AndresdoSantos
Comment options

@dai-shi
Comment options

@AndresdoSantos
Comment options

Answer selected by xypnox
Comment options

You must be logged in to vote
3 replies
@dai-shi
Comment options

@dai-shi
Comment options

@moklick
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants