Imports and Exports with TypeScript Modules

Imports and Exports with TypeScript Modules#

TypeScript Modules#

TypeScript has the concept of Modules, which encapuslate related code. Similar to ES Modules introduced in ES2015, TypeScript modules allow us to divide our code into several files.

Using modules, we can export and import runtime declarations (i.e. classes, variables, functions) as well as compile-time declarations (i.e type aliases and interfaces). For a file to be considered a module, it must contain at least one import or export statement.

To follow along the code in this section, we cannot use the TypeScript Playground as we need the ability to create files and run them locally. Refer to the first chapter for a recap on how to create and run TypeScript files from the commandline.

Named Exports and Imports#

We normally use named exports when we want to export multiple declarations from one file. In the example below, we have a numberUtils.ts that exports two utility functions and one constant:

 

This page is a preview of Beginners Guide to TypeScript

Please select a discussion on the left.