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

draft is typed as any when using produce() within React setState() #720

Closed
3 tasks done
oscar-b opened this issue Dec 9, 2020 · 3 comments
Closed
3 tasks done

draft is typed as any when using produce() within React setState() #720

oscar-b opened this issue Dec 9, 2020 · 3 comments
Labels

Comments

@oscar-b
Copy link

oscar-b commented Dec 9, 2020

🐛 Bug Report

draft is typed as any when using produce() within React setState(), instead of inferring the type. Older versions of immer does this properly, but newer one doesn't. As far as I can see, v2.1.5 (and before) works, after that it doesn't.

Link to repro

Working:
image
https://codesandbox.io/s/adoring-https-rsgqg?file=/src/App.tsx

Not working (only change is version of immer to v8.0.0):
image
https://codesandbox.io/s/flamboyant-lehmann-ms3fw?file=/src/App.tsx

Expected behavior

I expect the draft to get a proper type.

Environment

  • Immer version: 8.0.0
  • Typescript 4.0.3 (in Codesandbox)
  • I filed this report against the latest version of Immer
  • Occurs with setUseProxies(true)
  • Occurs with setUseProxies(false) (ES5 only)
@mweststrate
Copy link
Collaborator

Might suffer from the same problem as demonstrated in this playground, meaning we could possibly apply the same solution. See also: mobxjs/mobx#2740

@iChenLei
Copy link
Contributor

typedef change

Before v2.1.5 https://github.com/immerjs/immer/blob/v2.1.5/src/immer.d.ts#L71-L107
After v2.1.5 https://github.com/immerjs/immer/blob/v2.2.0/src/immer.d.ts#L72-L107
In your case, you match the Curried producer

> v2.1.5

	<
		Recipe extends (...args: any[]) => any,
		Params extends any[] = Parameters<Recipe>,
		T = Params[0]
	>(
		recipe: Recipe
	): <Base extends Immutable<T>>(
		base: Base,
		...rest: Tail<Params>
	) => Produced<Base, ReturnType<Recipe>>

recipe => Recipe => Recipe extends (...args: any[]) => any

<= v2.1.5

    /** Curried producer with no default value */
    <T = any, Rest extends any[] = [], Return = void>(
        recipe: (this: Draft<T>, draft: Draft<T>, ...rest: Rest) => Return
    ): (base: Immutable<T>, ...rest: Rest) => Produced<T, Return>

recipe => (this: Draft<T>, draft: Draft<T>, ...rest: Rest) => Return

@mweststrate Sir, Maybe this is why current version immer can't infer type correctly. I think it's not same with the mobx case.

append

  • The useState typedef
  function useState<S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>];
  type Dispatch<A> = (value: A) => void;
  type SetStateAction<S> = S | ((prevState: S) => S);

@mweststrate
Copy link
Collaborator

🎉 This issue has been resolved in version 9.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants