From 5e8568daa2efc91cdd59c70bdf5000d2ecfc58d6 Mon Sep 17 00:00:00 2001 From: Hugohong258 <282228220+Hugohong258@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:11:36 +0900 Subject: [PATCH] docs: explain the default state of curried producers --- website/docs/typescript.mdx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/website/docs/typescript.mdx b/website/docs/typescript.mdx index 73d227d3..deca9fb8 100644 --- a/website/docs/typescript.mdx +++ b/website/docs/typescript.mdx @@ -10,7 +10,8 @@ sidebar_label: TypeScript / Flow data-ea-type="image" className="horizontal bordered" > -
+ +
egghead.io lesson 12: Immer + TypeScript @@ -181,7 +182,7 @@ const toggler = produce(draft => { // typeof toggler = (state: Todo) => Todo ``` -However, in case the curried producer is defined with an initial state, Immer can infer the state type from the initial state, so in that case the generic doesn't need to be specified either: +Pass an initial state as the second argument to `produce` to let Immer infer the state type. The curried producer uses this state as its base when called without an argument or with `undefined`. A default value on the recipe's draft parameter is applied inside the recipe, after Immer has chosen the base, so it does not provide a default draft. ```typescript const state0: Todo = { @@ -194,7 +195,8 @@ const toggler = produce(draft => { draft.done = !draft.done }, state0) -// typeof toggler = (state: Todo) => Todo +// typeof toggler = (state?: Todo) => Todo +const next = toggler() // {title: "test", done: true} ``` In case the toggler has no initial state, and it has curried arguments, and you set the state generic explicitly, then type of any additional arguments should be defined explicitly as a tuple type as well: