unbuild icon indicating copy to clipboard operation
unbuild copied to clipboard

types inside .d.ts files are bugged

Open Zamiell opened this issue 2 years ago • 1 comments

Environment

"unbuild": "^2.0.0",
$ node --version
v20.9.0

Reproduction

If you use unbuild on the following code:

/* eslint-disable @typescript-eslint/no-explicit-any */

interface ReadonlyMapConstructor {
  new (): ReadonlyMap<any, any>;
  new <K, V>(
    entries?: ReadonlyArray<readonly [K, V]> | Iterable<readonly [K, V]> | null,
  ): ReadonlyMap<K, V>;
  readonly prototype: ReadonlyMap<any, any>;
}

/** An alias for the `Map` constructor that returns a read-only map. */
export const ReadonlyMap = Map as ReadonlyMapConstructor;

It produces the following output in a ".d.ts" file:

interface ReadonlyMapConstructor {
    new (): ReadonlyMap$1<any, any>;
    new <K, V>(entries?: ReadonlyArray<readonly [K, V]> | Iterable<readonly [K, V]> | null): ReadonlyMap$1<K, V>;
    readonly prototype: ReadonlyMap$1<any, any>;
}
/** An alias for the `Map` constructor that returns a read-only map. */
declare const ReadonlyMap$1: ReadonlyMapConstructor;

There are two problems with this:

  1. If we import the library and do const FOO = new ReadonlyMap<string, string>(); and we mouse over FOO, we see that the type is ReadonlyMap$1<K, V> instead of ReadonlyMap<K, V>. That's a problem. In other words, if we are exporting helper types to consumers, we don't want those types mangled.
  2. More importantly, the "@typescript-eslint/no-unsafe-assignment" rule flags this as an any value, because the ".d.ts" has errors in it.

Zamiell avatar Dec 15 '23 02:12 Zamiell

Can you confirm it with the latest unbuild? I tried it, the current output is:

interface ReadonlyMapConstructor {
    new (): ReadonlyMap<any, any>;
    new <K, V>(entries?: ReadonlyArray<readonly [K, V]> | Iterable<readonly [K, V]> | null): ReadonlyMap<K, V>;
    readonly prototype: ReadonlyMap<any, any>;
}
/** An alias for the `Map` constructor that returns a read-only map. */
declare const ReadonlyMap: ReadonlyMapConstructor;

export { ReadonlyMap };

It seems that this issue has been resolved.

kricsleo avatar Feb 08 '25 03:02 kricsleo