Description
🔍 Search Terms
import attributes, ambient module, declare module with, wildcard module, pattern, typeof import, matched module
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ 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
- 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.
- 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. - What workarounds are you using in the meantime? A cast to
ModuleUrl<typeof import("./same/path")>, wrapped in a typed identity function.
Related:
- A way to ignore prefix/suffix in module ID, then resolve the remaining ID as usual #37319 (resolve what's left of a wildcard as usual)
- Ambient Module Declarations for Import Attributes (formerly known as Import Assertions) #46135 (ambient modules for import attributes)
- Content mappers: pass an import's query or attributes to
transform#64912 (content mappers seeing an import's attributes: it would let a mapper type its own files differently by attribute, but not a declaration refer to any file's plain module).
Activity
feat: type the MDX pages by their modules