Skip to content

infer the wrong type when returntype of produce callback is any #785

Closed
@hardfist

Description

@hardfist

🐛 Bug Report

import { produce, Draft } from 'immer';
const x = { a: 1 };

function wrapper<T>(s: T, fn: (d: Draft<T>) => void) {
  const result = produce(s, (y) => {
    return fn(y);
  });
  return result;
}

function wrapper2<T>(s: T, fn: (d: Draft<T>) => any) {
  const result = produce(s, (y) => {
    return fn(y);
  });
  return result;
}
const res = wrapper(x, (y) => {
  y.a = 1;
}); // {a:number}
const res2 = wrapper2(x, (y) => {
  y.a = 2;
}); // Promise<{a:number}>
res2.then(x => {
    console.log('x:',x) // crash
})
console.log(x, res, res2);

Link to repro

https://www.typescriptlang.org/play?#code/PTAEAEBcGcFoGMAWBTeBrAUASwLYAcB7AJ0lAG9Q8iCATAV3mQBpQARIgQwDNSBfULtRygA5LhzIiIgNwZ4BAHbRSAD1ABecqA4AuUAEZQvWfKWkAnhsrV6jABQqWdmgEoNAPnIZQoGgDoOKwAmWV4XWS46BXhILEVQAHdOPDxJAB4AFXc7aD0Mli4FPWc9dm5ITPc3dU8ANwIsVy8fU2VQImRoOgAbUk0qWgZkHKdzas8ybx925Eg6IgUBBTsx2R8wtZm5hZmu3tCMDEjo2PikjhTJIMqcvIKi0BK2Th5K8e0FMebQVtIOvb61kG9mgo3ek2mW3mi0KK3CUw2Uw620W-x6kAOv12VnOlyIDjBHm+5gCVn0oXCoBA5F0CjoOAARpJeHJFG1-kEcclUkQggTHl8asTSZoQhgNlSwAAFIRYaDINJkWn0plEXjuVlKAjdZB+boEADm-PMLH+ps6QXhQA

Activity

added 2 commits that reference this issue on Apr 19, 2021

fix: immerjs#785 fix type inference for produce

9c0419c

fix: immerjs#785 fix type inference for produce

2ee9859
mweststrate

mweststrate commented on Apr 25, 2021

@mweststrate
Collaborator

🎉 This issue has been resolved in version 9.0.2 🎉

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @mweststrate@hardfist

        Issue actions

          infer the wrong type when returntype of produce callback is any · Issue #785 · immerjs/immer