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

Issue to index generic record with generic key value in recipe due to draft wrapper #1135

Open
1 of 3 tasks
weilbith opened this issue Jul 25, 2024 · 0 comments
Open
1 of 3 tasks

Comments

@weilbith
Copy link

weilbith commented Jul 25, 2024

🐛 Bug Report

Using generic function arguments around produce causes type issues. Its probably best explained with an example (link to CodeSandbox below):

enum Foo {
  One,
  Two,
}

type Bar<F extends Foo> = Readonly<Record<F, number>>;

function modifyBar<F extends Foo>(bar: Bar<F>, foo: F, value: number): Bar<F> {
  return produce(bar, (draft) => {
    draft[foo] = value; // <-- Issue here
  });
}

The type error at the marked line is then: Type 'F' cannot be used to index type 'Draft<Readonly<Record<F, number>>>'.
A current workaround is to make the type Bar actually mutable (i.e. remove the Readonly wrapper) and then manually cast the recipe like (draft: Bar<F>) => { /* ... */ }. Both do not feel good or are not possible for the production circumstances.

Are there any alternatives to solve this properly?

Link to repro

Link to CodeSandbox

To Reproduce

Just inspect the error shown in the code editor.

Observed behavior

Type error that correct key type can't be used to index a Record wrapped as Draft.

Expected behavior

No type error and the produce just works as without a recipe.

Environment

We only accept bug reports against the latest Immer version.

  • Immer version: v10.1.1
  • I filed this report against the latest version of Immer
  • Occurs with setUseProxies(true) (I don't know how to apply this, sorry)
  • Occurs with setUseProxies(false) (ES5 only)
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

1 participant