immerjs / immer

Create the next immutable state by mutating the current one
https://immerjs.github.io/immer/
MIT License
27.5k stars 850 forks source link

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 #1115

Closed JodoZT closed 4 months ago

JodoZT commented 5 months ago

🐛 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): Draft { return t as Draft }

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): Draft; // 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): Draft; // Here it should be WritableDraft instead of raw definition of type A export declare const draftA: WritableDraft;



## Environment
immer@^10

- **Immer version:**
- [x] I filed this report against the _latest_ version of Immer