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

type WritableDraft is not exported anywhere in dist, this causes build type declaration files with Draft finally fall back to raw and causing the type declaration exceeds max token length

JodoZT opened this issue Β· comments

commented

πŸ› Bug Report

A clear and concise description of what the bug is.
When using Draft with object type in our code and build type declaration files from tsc, it will extract the T object type to raw object type because the WritableDraft is not exported from immer dist.

Link to repro

A bug report without a reproduction is not a bug report. Failing to follow this templately is likely to result in an immediate close & lock of the issue.

CodeSandbox link
Run pnpm run build-types to build type declarations to types folder

To Reproduce

Steps to reproduce the behavior:

  1. import Draft type from immer
  2. Write function with Draft type
  3. Build type declaration for the .ts file
import type { Draft } from 'immer'

export interface A {
  a: number
}

export function draft<T>(t: T): Draft<T> {
  return t as Draft<T>
}

const a: A = {
  a: 1,
}

export const draftA = draft(a)

Observed behavior

The generated type declaration file is:

import type { Draft } from 'immer';
export interface A {
    a: number;
}
export declare function draft<T>(t: T): Draft<T>;
// Here it uses raw A definition, which can be very long
export declare const draftA: {
    a: number;
};

Expected behavior

import type { Draft, WritableDraft } from 'immer';
export interface A {
    a: number;
}
export declare function draft<T>(t: T): Draft<T>;
// Here it should be WritableDraft<A> instead of raw definition of type A
export declare const draftA: WritableDraft<A>;

Environment

immer@^10

  • Immer version:
  • I filed this report against the latest version of Immer