immerjs / immerjs/immer

The return type of the produce method will have an additional undefined

Open
#1,003 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
JavaScript
Stars
29k
Forks
881
PR merge metrics
No merged PRs in 30d

Description

🙋‍♂ Question

A clear and concise description of what the question is. In general we recommend to use Stack Overflow for questions. Questions typically are processed with less priority.

import { produce } from 'immer';

const func = produce((state: Record<string, string> = {}): Record<string, string> => {
  return state;
});

In the above code, I expect the type of the func to be Record<string, string>, but the actual type check returns Record<string, string> | undefined, how can I avoid the undefined?

image

This problem only occurs in 9.x versions

Link to repro

https://codesandbox.io/s/immer-sandbox-forked-n1okz2?file=/src/index.ts

Environment

We only accept questions against the latest Immer version.

  • Immer version:
  • Occurs with setUseProxies(true)
  • Occurs with setUseProxies(false) (ES5 only)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the TypeScript example in the linked CodeSandbox, especially src/index.ts and its produce call, and reproduce the reported inferred return type. Compare the behavior with the stated expectation for the 9.x versions. Done means the example's type check no longer adds undefined to the returned Record type.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.