unimport icon indicating copy to clipboard operation
unimport copied to clipboard

When a file only contains global declarations, it is not auto-imported

Open Anoesj opened this issue 7 months ago • 3 comments

Environment

unimport: 5.0.0 node: 22.14

Reproduction

https://stackblitz.com/edit/github-av9ljigg?file=.nuxt%2Ftypes%2Fimports.d.ts

After the project is installed and the dev server has started, go to file .nuxt/types/imports.d.ts.

Describe the bug

When a file only contains declare global, but does not contain any type exports or regular exports, the file is skipped during auto-import. As soon as a regular or type export is added to that file, it is auto-imported and the global declaration becomes available in the type layer of the app.

Repro explanation

In the repro, which is a basic Nuxt project setup, I manually auto-import the following three files:

1. a.ts

export const a = 'a';
export type A = typeof a;

2. b.ts

export const b = 'b';

declare global {
  type B = typeof b;
}

3. c.ts

export {};

declare global {
  type C = 'C';
}

This has the following effect on .nuxt/types/imports.d.ts (irrelevant stuff is left out):

// Generated by auto imports
export {}
declare global {
  const a: typeof import('../../extra/a')['a']
  const b: typeof import('../../extra/b')['b']
}

// for type re-export
declare global {
  // @ts-ignore
  export type { A } from '../../extra/a'
  import('../../extra/a')
}

import { UnwrapRef } from 'vue'
declare module 'vue' {
  interface ComponentCustomProperties {
    readonly a: UnwrapRef<typeof import('../../extra/a')['a']>
    readonly b: UnwrapRef<typeof import('../../extra/b')['b']>
  }
}

As you can see, c.ts is entirely ignored, while global declarations of b.ts are included, because it contains a regular export.

Additional context

No response

Logs


Anoesj avatar May 04 '25 09:05 Anoesj

Just curious, should they be auto-imported? If they are global, they should be usable directly, even without importing.

ilyaliao avatar May 20 '25 15:05 ilyaliao

Image

Your repro seems to work fine for me. What do you expect? The c.ts does not export any thing soi it's not included - which is working as expected

antfu avatar May 21 '25 07:05 antfu

Edited: I expected c.ts to be auto-imported too. If the original file is not included in the tsconfig of the app (in include), the globally declared type is not available in the app. Therefore, I would expect auto-import to add the file to the generated .d.ts file.

Anoesj avatar May 21 '25 11:05 Anoesj