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:

  1. // module.d.ts
  2. export declare class Pet {
  3. name: string;
  4. }

Can be used in a non-module file global-script.ts:

  1. // global-script.ts
  2. function adopt(p: import("./module").Pet) {
  3. console.log(`Adopting ${p.name}...`);
  4. }

This also works in JSDoc comments to refer to types from other modules in .js:

  1. // a.js
  2. /**
  3. * @param p { import("./module").Pet }
  4. */
  5. function walk(p) {
  6. console.log(`Walking ${p.name}...`);
  7. }