Modules

As TypeScript projects grow, organizing code efficiently becomes essential.

To do this, we think of each file as a module which can export functions and variables to other modules.

Loading TypeScript...

When you use the import and export keyword, TypeScript considers the file to be a module.

Imports and exports

A TypeScript file can use the export keyword to allow specified functions and variables to be accessed from other files with the import keyword.

Loading TypeScript...

You can also import all exported values from another module into a single name with the import * as syntax.

Loading TypeScript...

Imports from other files can also be renamed.

Loading TypeScript...

TODO

  • An import can be renamed (import { x as y })
  • Mix and match (import x, { y as z })
  • Import without variables (import "foo")
  • Imported object value can be mutated
  • Import type
  • Mix and match with import - import { foo, type bar }
  • CommonJS syntax with require
  • Exporting from another file and export star

Examples:

  • importing constants
  • collecting imports in one file
  • circular imports

Was this page helpful?