TypeScriptModules and namespaces
Named exports and selective imports in TypeScript
3 Several named exports, imported selectively
easyWrite a module exporting a few functions and values by name. In another file import only some of them and use those.
Stuck? Three hints, smallest first
- 1. You can also write one
export { a, b, c }statement at the bottom instead of marking each declaration. - 2. Importing only what you need is what lets a bundler drop the rest.
- 3.
import * as namepulls everything in under one object, which is handy but blocks that trimming.
The real answer, across 2 files. Type-checked by the compiler at build time; not runnable in a browser, because a panel cannot contain import or export.
export const PARK = 'Jibowu';
export const MAX_SEATS = 59;
export function isPlate(text: string): boolean {
return /^[A-Z]{3}-\d{3}-[A-Z]{2}$/.test(text);
}
export function fare(seats: number, luxury = false): number {
return seats * (luxury ? 18000 : 12000) + 200;
}
function internalOnly(): string {
return 'not exported';
}
// The alternative spelling: one list at the bottom.
export { internalOnly as debugOnly };
// Only what this file actually uses.
import { fare, isPlate } from './fleet.js';
console.log(isPlate('LAG-441-XA')); // true
console.log(fare(2)); // 24200
console.log(fare(2, true)); // 36200
// Everything at once, under one name, when you really want it all:
import * as fleet from './fleet.js';
console.log(fleet.PARK, fleet.MAX_SEATS);
const same = (a: unknown, b: unknown): boolean =>
JSON.stringify(a) === JSON.stringify(b);
function test(name: string, got: unknown, want: unknown): void {
console.log(
(same(got, want) ? 'ok ' : 'FAIL ') + name +
(same(got, want) ? '' : ` got ${JSON.stringify(got)}, want ${JSON.stringify(want)}`)
);
}
// The same members, gathered on one object so this can run.
const PARK = 'Jibowu';
const MAX_SEATS = 59;
function isPlate(text: string): boolean {
return /^[A-Z]{3}-\d{3}-[A-Z]{2}$/.test(text);
}
function fare(seats: number, luxury = false): number {
return seats * (luxury ? 18000 : 12000) + 200;
}
// This object plays the part of the module's public surface.
const fleet = { PARK, MAX_SEATS, isPlate, fare };
// And this plays the part of a selective import.
const { fare: fareOnly, isPlate: checkPlate } = fleet;
console.log(checkPlate('LAG-441-XA'), checkPlate('nope'));
console.log(fareOnly(2), fareOnly(2, true));
console.log(fleet.PARK, fleet.MAX_SEATS);
test('a valid plate', checkPlate('LAG-441-XA'), true);
test('an invalid one', checkPlate('nope'), false);
test('economy fare for two', fareOnly(2), 24200);
test('luxury fare for two', fareOnly(2, true), 36200);
test('the constants came through', fleet.PARK, 'Jibowu');
test('the surface has exactly four members', Object.keys(fleet).length, 4);
Output
The export { internalOnly as debugOnly } line in the reference is worth spotting: you can rename on the way out, and rename again on the way in with import { debugOnly as d }. Handy when two modules would otherwise collide on a name.