Skip to content

Let an ambient module declaration with import attributes refer to the module it matched #65109

Open

Description

🔍 Search Terms

import attributes, ambient module, declare module with, wildcard module, pattern, typeof import, matched module

✅ Viability Checklist

⭐ Suggestion

TS 7.1 lets a declaration type imports by their attributes (#63931), e.g. an import that gives the module's URL instead of the module (a loader hook answers it, without loading the module):

type ModuleUrl<Module = unknown> = URL & { readonly __module?: Module };

declare module "*" with { type: "url" } {
  const url: ModuleUrl;
  export default url;
}

The declaration can't say anything about the module the specifier resolves to, though. When the attribute gives something derived from that module (a handle to it, a lazy or deferred version of it, a wrapper around its exports), its declaration needs the module's own type, and there's no way to write it: typeof import(...) needs the path written again.

Some way to name the matched module's type inside the declaration, e.g.:

declare module "*" with { type: "url" } {
  // The module the importing specifier resolves to, as a plain import would type it.
  const url: ModuleUrl<typeof import("*")>;
  export default url;
}

The syntax is only illustrative. What matters is that it resolves the importing specifier as usual (as if imported without the attribute), so it's typed exactly as a plain import would be.

📃 Motivating Example

A module is imported by URL so it's only loaded when (and if) it's needed:

// plugins/markdown.ts
export function render(source: string): string {
  // ...
}
// main.ts
import markdown from "./plugins/markdown.ts" with { type: "url" };

declare function lazyImport<Module>(url: ModuleUrl<Module>): Promise<Module>;

const plugin = await lazyImport(markdown); // `unknown`: the declaration can't say more
plugin.render("# Hi"); // error TS18046: 'plugin' is of type 'unknown'.

typeof import("./plugins/markdown.ts") has exactly the right type, but only by writing the path a second time:

const plugin = await lazyImport(
  markdown as ModuleUrl<typeof import("./plugins/markdown.ts")>,
);

...which is unchecked (nothing ties the two paths together) and easy to get wrong when copying a line.

💻 Use Cases

  1. What do you want to use this for? Typing imports whose attributes give a handle to, or a transformation of, the module itself: module URLs as above, deferred or lazy modules, worker or "client reference" modules in bundlers, and the like.
  2. What shortcomings exist with current approaches? The path has to be repeated in a typeof import(...), and the two can silently drift apart. A pattern declaration can only give the same type to every file it matches.
  3. What workarounds are you using in the meantime? A cast to ModuleUrl<typeof import("./same/path")>, wrapped in a typed identity function.

Related:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Suggestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions