immerjs / immer

Create the next immutable state by mutating the current one

Home Page:https://immerjs.github.io/immer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

export `Objectish` type

macmillen opened this issue Β· comments

πŸš€ Feature Proposal

I created a wrapper for immer in Svelte to enhance its capabilities and I am using a generic type for that. The problem is that applyPatches accepts a type that extends the Objectish type which I cannot import. I propose to export this type.

Can this be solved in user-land code?

I can copy paste the type from the source code.

Example

import { applyPatches, enablePatches } from "immer";

// I need to duplicate this because there is no export in immer
type Objectish = { [key: string]: unknown } | Array<unknown> | Set<unknown> | Map<unknown, unknown>;

enablePatches();

export const createImmerStore = <T extends Objectish>(obj: T) => {
  let state = obj;

  const draftStore = {
    // ...
    undo() {
      // if I don't use `T extends Objectish` it throws an error
      state = applyPatches(state, undo.inversePatches);
    },
  };

  // ...
};