import types
Modules can import types declared in other modules. But non-module global scripts cannot access types declared in modules. Enter import
types.
Using import("mod")
in a type annotation allows for reaching in a module and accessing its exported declaration without importing it.
Example
Given a declaration of a class Pet
in a module file:
// module.d.ts
export declare class Pet {
name: string;
}
Can be used in a non-module file global-script.ts
:
// global-script.ts
function adopt(p: import("./module").Pet) {
console.log(`Adopting ${p.name}...`);
}
This also works in JSDoc comments to refer to types from other modules in .js
:
// a.js
/**
* @param p { import("./module").Pet }
*/
function walk(p) {
console.log(`Walking ${p.name}...`);
}