Any declaration (variable, const, function, class, etc.) can be exported from module to be imported in other module.
Typescript offer two export types: named and default.
Named export
// adams.ts export function hello(name: string){ console.log(`Hello ${name}!`); } export const answerToLifeTheUniverseAndEverything = 42; export const unused = 0;
When importing named exports, you can specify which elements you want to import.
import {hello, answerToLifeTheUniverseAndEverything} from "./adams"; hello(answerToLifeTheUniverseAndEverything); // Hello 42!
Default export
Each module can have one default export
// dent.ts const defaultValue = 54; export default defaultValue;
which can be imported using
import dentValue from "./dent"; console.log(dentValue); // 54
Bundled import
Typescript offers method to import whole module into variable
// adams.ts export function hello(name: string){ console.log(`Hello ${name}!`); } export const answerToLifeTheUniverseAndEverything = 42;
import * as Bundle from "./adams"; Bundle.hello(Bundle.answerToLifeTheUniverseAndEverything); // Hello 42! console.log(Bundle.unused); // 0