Skip to content

Commit

Permalink
Improve types in Immer middleware docs (#2139)
Browse files Browse the repository at this point in the history
* Having the generic type on `immer` makes you lose introspection in IDE's.

* Add note about parenthesis and link back to typescript guide.
  • Loading branch information
Brammm authored Oct 22, 2023
1 parent 83b86a7 commit 643bb7a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/guides/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ nav: 8

## Basic usage

The difference when using TypeScript is that instead of writing `create(...)`, you have to write `create<T>()(...)` (notice the extra parenthesis `()` too along with the type parameter) where `T` is the type of the state to annotate it. For example:
The difference when using TypeScript is that instead of writing `create(...)`, you have to write `create<T>()(...)` (notice the extra parentheses `()` too along with the type parameter) where `T` is the type of the state to annotate it. For example:

```ts
import { create } from 'zustand'
Expand Down
10 changes: 6 additions & 4 deletions docs/integrations/immer-middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ npm install immer

## Usage

(Notice the extra parentheses after the type parameter as mentioned in the [Typescript Guide](../guides/typescript.md)).

Updating simple states

```ts
Expand All @@ -34,8 +36,8 @@ type Actions = {
decrement: (qty: number) => void
}

export const useCountStore = create(
immer<State & Actions>((set) => ({
export const useCountStore = create<State & Actions>()(
immer((set) => ({
count: 0,
increment: (qty: number) =>
set((state) => {
Expand Down Expand Up @@ -69,8 +71,8 @@ type Actions = {
toggleTodo: (todoId: string) => void
}

export const useTodoStore = create(
immer<State & Actions>((set) => ({
export const useTodoStore = create<State & Actions>()(
immer((set) => ({
todos: {
'82471c5f-4207-4b1d-abcb-b98547e01a3e': {
id: '82471c5f-4207-4b1d-abcb-b98547e01a3e',
Expand Down

1 comment on commit 643bb7a

@vercel
Copy link

@vercel vercel bot commented on 643bb7a Oct 22, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.