Skip to content

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

@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

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

    Issue actions