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

Custom methods missing in Draft<T> when T implements Map #864

Closed
3 tasks done
Yaojian opened this issue Oct 31, 2021 · 1 comment
Closed
3 tasks done

Custom methods missing in Draft<T> when T implements Map #864

Yaojian opened this issue Oct 31, 2021 · 1 comment
Labels

Comments

@Yaojian
Copy link

Yaojian commented Oct 31, 2021

🐛 Bug Report

Custom methods defined in T is missing in Draft<T> when T implements (not extends) Map.

Related to #502 , #781

Link to repro

https://codesandbox.io/s/immer-sandbox-forked-3n014

To Reproduce

  describe("When the custom map implements Map", () => {
    class CustomMap implements Map<number, string> {
      [immerable] = true;

      count(): number {
        return this.internalMap.size;
      }

      //#region Map<number, string> implementation
      // .....
      //#endregion
    }

    const base = new CustomMap();
    base.set(1, "first");

    it("the draft should be type compatible with CustomMap", () => {
      produce(base, (draft) => {
        // draft.count(); // TS2339: Property 'count' does not exist on type 'Map<number, string>'.
      });
    });

    it("draft can be hacked with type intersection", () => {
      let count: number | undefined = undefined;
      produce<CustomMap, Draft<CustomMap> & CustomMap>(base, (draft) => {
        count = draft.count(); //OK
      });
      expect(count).toBe(1);
    });
  });

Observed behavior

Draft<CustomMap> is Map<number, string> without CustomMap.count defined.

Expected behavior

Draft<CustomMap> should contain the count property.

Environment

We only accept bug reports against the latest Immer version.

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

That is correct, Immer does not support customised array, Map or Set functionality beyond their standard functionality as per docs:

Immer does not support exotic / engine native objects such as DOM Nodes or Buffers, nor is subclassing Map, Set or arrays supported and the immerable symbol can't be used on them.

https://immerjs.github.io/immer/complex-objects

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

2 participants