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

Upgraded to 1.7.2, typescript typedefs changes failing build #205

Closed
3 tasks done
scriby opened this issue Sep 28, 2018 · 6 comments
Closed
3 tasks done

Upgraded to 1.7.2, typescript typedefs changes failing build #205

scriby opened this issue Sep 28, 2018 · 6 comments

Comments

@scriby
Copy link
Contributor

scriby commented Sep 28, 2018

  • Issue: Upgraded to 1.7.2, typescript typedefs changes failing build
    • Version: 1.7.2
    • SIMPLE Reproduction:

Attempting to build Typescript code that looks like this doesn't work anymore:

import {produce, Draft} from 'immer';

const producer = produce((draft: Draft<State>, action: Action) => {
  draft.xyz = action.xyz;
});

This code built previously under 1.2.1, but does not build under 1.7.2 (only change is I changed State to Draft<State>). Typescript is complaining that "void" is not assignable to type DraftObject.

I think it's supposed to be matching this type def:

// 1 additional argument of type A
<S = any, A = any>(recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void|S): 
    (currentState: S, a: A) => S

I think the issue is that the typedef declares that the recipe function will return an "S", but that's going to be difficult because it receives a Draft<S>. Tweaking the type def locally and having recipe return void|Draft<S> fixes the issue for me.

  • Expected behavior: It builds
  • Observed behavior: It doesn't build
  • Occurs when using Proxies (use setUseProxies(true))
  • Occurs in the ES5 implementation (use setUseProxies(false))
@scriby
Copy link
Contributor Author

scriby commented Sep 28, 2018

As an update to this, the old style type defs work better for my codebase:

<S = any>(
      currentState: S, recipe: (this: S, draftState: S) => void|S,
      listener?: PatchListener): S

// 1 additional argument of type A
<S = any, A = any>(recipe: (this:S, draftState:S, a: A) => void|S): 
    (currentState: S, a: A) => S

The code doesn't really need to use the Draft state, so it's easier just to leave it like this. Not sure if you still consider these "valid" or not, but they still look ok to me.

@mweststrate
Copy link
Collaborator

@scriby can you demonstrate this in a sandbox, because if I read your example correctly, your recipe returns void, not S, so that should work out fine?

@aleclarson
Copy link
Member

aleclarson commented Oct 13, 2018

@scriby What version of TypeScript are you using?

Repro: https://codesandbox.io/s/x7y885rlww (I don't know which TS version is in CodeSandbox)

My fix passes in TypeScript playground, but I don't know which TS version is used there.

It also passes locally using typescript v3.1.0.

Strangely, the repro code also passes in TypeScript playground.

@aleclarson aleclarson added the bug label Oct 13, 2018
@aleclarson
Copy link
Member

aleclarson commented Oct 13, 2018

The problem was that the generic S was being inferred from the return value of the recipe function.

aleclarson added a commit that referenced this issue Oct 13, 2018
Fixes #205

I don't think this works under TypeScript 3.0 (or even 3.1)
@aleclarson aleclarson self-assigned this Oct 13, 2018
@scriby
Copy link
Contributor Author

scriby commented Oct 15, 2018

Since filing this issue I've noticed the TS version at work doesn't (or doesn't always?) treat void and undefined as the same type. We're on a little older version, so that might be the cause. I'll go ahead and close for now until we get on a more recent version.

@scriby scriby closed this as completed Oct 15, 2018
aleclarson added a commit that referenced this issue Oct 28, 2018
aleclarson added a commit that referenced this issue Dec 15, 2018
aleclarson added a commit that referenced this issue Dec 15, 2018
aleclarson added a commit that referenced this issue Dec 15, 2018
aleclarson added a commit that referenced this issue Dec 15, 2018
aleclarson added a commit that referenced this issue Dec 15, 2018
aleclarson added a commit that referenced this issue Dec 15, 2018
aleclarson added a commit that referenced this issue Dec 15, 2018
aleclarson added a commit that referenced this issue Dec 15, 2018
aleclarson added a commit to aleclarson/immer that referenced this issue Dec 15, 2018
@aleclarson
Copy link
Member

🎉 This issue has been resolved in version 1.9.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
Projects
None yet
Development

No branches or pull requests

3 participants