Skip to content

Typings of imported namespaces/ESM modules are incomplete/broken #473

@Anoesj

Description

@Anoesj

Reproduction

A Nuxt repro, where unimport is used under the hood by unplugin-auto-import: https://github.com/Anoesj/unimport-namespaces

As described in the repo's README:

  1. pnpm i && pnpm dev
  2. See lots of errors in app.vue
  3. Manually edit .nuxt/types/imports.d.ts:
// Replace this line:
const z: typeof import('../../app/utils/zod')['z']
// With this line:
export { z } from '../../app/utils/zod'
  1. See errors go away (except the one at typeof z.core.$ZodIssue which is expected)

Describe the bug

With the current way unimport declares exports of auto-imports in the .d.ts file, problems like the following one happen a lot with namespaces (are ESM module exports considered namespaces too?):

globally-shared/validation/zod.ts:

export * as z from 'zod';

The generated .d.ts file:

declare global {
  const z: typeof import('../../../../globally-shared/validation/zod')['z']
}

Some .ts file that uses the auto-imports:

type ZodIssue = z.core.$ZodIssue;
//              ^ Cannot find namespace 'z'.

type ZodIssuePlease = typeof z.core.$ZodIssue;
//                                  ^ Property '$ZodIssue' does not exist on type 'typeof import(...)'.

/*
  NOTE: The only workaround that works for the issue above is to add
  `export type $ZodIssue = z.core.$ZodIssue;`  in `globally-shared/validation/zod.ts`.
  That's bad DX though, especially if `z.core.$ZodIssue` would have had generics
  that you'd need to redefine!
*/

const aString: z.ZodType = z.string();
//             ^ Cannot find namespace 'z'.

const aStringWorkaround: InstanceType<typeof z.ZodType> = z.string();
// Works as a workaround, but this sucks...

When you (manually) change the notation in the .d.ts file unimport generates to...

declare global {
  export { z } from '../../../../globally-shared/validation/zod'
}

...everything works perfectly without any need for workarounds.

Additional context

No response

Logs

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions