External modules

There is a lot of power and usability packed into the TypeScript external module pattern. Here we discuss its power and some patterns needed to reflect real world usages.

Clarification: commonjs, amd, es modules, others

First up we need to clarify the (awful) inconsistency of the module systems out there. I’ll just give you my current recommendation and remove the noise i.e. not show you all the other ways things can work.

From the same TypeScript you can generate different JavaScript depending upon the module option. Here are things you can ignore (I am not interested in explaining dead tech):

  • AMD: Do not use. Was browser only.
  • SystemJS: Was a good experiment. Superseded by ES modules.
  • ES Modules: Not ready yet.

Now these are just the options for generating the JavaScript. Instead of these options use module:commonjs

How you write TypeScript modules is also a bit of a mess. Again here is how not to do it today:

  • import foo = require('foo'). i.e. import/require. Use ES module syntax instead.

Cool, with that out of the way, lets look at the ES module syntax.

Summary: Use module:commonjs and use the ES module syntax to import / export / author modules.

ES Module syntax

  • Exporting a variable (or type) is as easy as prefixing the keyword export e.g.
  1. // file `foo.ts`
  2. export let someVar = 123;
  3. export type SomeType = {
  4. foo: string;
  5. };
  • Exporting a variable or type in a dedicated export statement e.g.
  1. // file `foo.ts`
  2. let someVar = 123;
  3. type SomeType = {
  4. foo: string;
  5. };
  6. export {
  7. someVar,
  8. SomeType
  9. };
  • Exporting a variable or type in a dedicated export statement with renaming e.g.
  1. // file `foo.ts`
  2. let someVar = 123;
  3. export { someVar as aDifferentName };
  • Import a variable or a type using import e.g.
  1. // file `bar.ts`
  2. import { someVar, SomeType } from './foo';
  • Import a variable or a type using import with renaming e.g.
  1. // file `bar.ts`
  2. import { someVar as aDifferentName } from './foo';
  • Import everything from a module into a name with import * as e.g.

    1. // file `bar.ts`
    2. import * as foo from './foo';
    3. // you can use `foo.someVar` and `foo.SomeType` and anything else that foo might export.
  • Import a file only for its side effect with a single import statement:

  1. import 'core-js'; // a common polyfill library
  • Re-Exporting all the items from another module
  1. export * from './foo';
  • Re-Exporting only some items from another module
  1. export { someVar } from './foo';
  • Re-Exporting only some items from another module with renaming
  1. export { someVar as aDifferentName } from './foo';

Default exports/imports

As you will learn later, I am not a fan of default exports. Neverthless here is syntax for export and using default exports

  • Export using export default
    • before a variable (no let / const / var needed)
    • before a function
    • before a class
  1. // some var
  2. export default someVar = 123;
  3. // OR Some function
  4. export default function someFunction() { }
  5. // OR Some class
  6. export default class SomeClass { }
  • Import using the import someName from "someModule" syntax (you can name the import whatever you want) e.g.
  1. import someLocalNameForThisFile from "../foo";

Module paths

I am just going to assume moduleResolution: commonjs. This is the option you should have in your TypeScript config. This setting is implied automatically by module:commonjs.

There are two distinct kinds of modules. The distinction is driven by the path section of the import statment (e.g. import foo from 'THIS IS THE PATH SECTION').

  • Relative path modules (where path starts with . e.g. ./someFile or ../../someFolder/someFile etc.)
  • Other dynamic lookup modules (e.g. 'core-js' or 'typestyle' or 'react' or even 'react/core' etc.)

The main difference is how the module is resolved on the file system.

I will use a conceptual term place that I will explain after mentioning the lookup pattern.

Relative path modules

Easy, just follow the relative path :) e.g.

  • if file bar.ts does import * as foo from './foo'; then place foo must exist in the same folder.
  • if file bar.ts does import * as foo from '../foo'; then place foo must exist in a folder up.
  • if file bar.ts does import * as foo from '../someFolder/foo'; then one folder up, there must be a folder someFolder with a place foo

Or any other relative path you can think of :)

Dynamic lookup

When the import path is not relative, lookup is driven by node style resolution. Here I only give a simple example:

  • You have import * as foo from 'foo', the following are the places that are checked in order

    • ./node_modules/foo
    • ../node_modules/foo
    • ../../node_modules/foo
    • Till root of file system
  • You have import * as foo from 'something/foo', the following are the places that are checked in order

    • ./node_modules/something/foo
    • ../node_modules/something/foo
    • ../../node_modules/something/foo
    • Till root of file system

What is place

When I say places that are checked I mean that the following things are checked in that place. e.g. for a place foo:

  • If the place is a file, e.g. foo.ts, hurray!
  • else if the place is a folder and there is a file foo/index.ts, hurray!
  • else if the place is a folder and there a foo/package.json and there is a file specified in the types key in the package.json that exists, then hurray!
  • else if the place is a folder and there a package.json and there is a file specified in the main key in the package.json that exists, then hurray!

By file I actually mean .ts / .d.ts and .js.

And that’s it. You are now module lookup experts (not a small feat!).

Overturning dynamic lookup just for types

You can declare a module globally for your project by using declare module 'somePath' and then imports will resolve magically to that path

e.g.

  1. // globals.d.ts
  2. declare module 'foo' {
  3. // Some variable declarations
  4. export var bar: number; /*sample*/
  5. }

and then:

  1. // anyOtherTsFileInYourProject.ts
  2. import * as foo from 'foo';
  3. // TypeScript assumes (without doing any lookup) that
  4. // foo is {bar:number}

import/require for importing type only

The following statement:

  1. import foo = require('foo');

actually does two things:

  • Imports the type information of the foo module.
  • Specifies a runtime dependency on the foo module.

You can pick and choose so that only the type information is loaded and no runtime dependency occurs. Before continuing you might want to recap the declaration spaces section of the book.

If you do not use the imported name in the variable declaration space then the import is completely removed from the generated JavaScript. This is best explained with examples. Once you understand this we will present you with use cases.

Example 1

  1. import foo = require('foo');

will generate the JavaScript:

That’s right. An empty file as foo is not used.

Example 2

  1. import foo = require('foo');
  2. var bar: foo;

will generate the JavaScript:

  1. var bar;

This is because foo (or any of its properties e.g. foo.bas) is never used as a variable.

Example 3

  1. import foo = require('foo');
  2. var bar = foo;

will generate the JavaScript (assuming commonjs):

  1. var foo = require('foo');
  2. var bar = foo;

This is because foo is used as a variable.

Use case: Lazy loading

Type inference needs to be done upfront. This means that if you want to use some type from a file foo in a file bar you will have to do:

  1. import foo = require('foo');
  2. var bar: foo.SomeType;

However you might want to only load the file foo at runtime under certain conditions. For such cases you should use the imported name only in type annotations and not as a variable. This removes any upfront runtime dependency code being injected by TypeScript. Then manually import the actual module using code that is specific to your module loader.

As an example, consider the following commonjs based code where we only load a module 'foo' on a certain function call:

  1. import foo = require('foo');
  2. export function loadFoo() {
  3. // This is lazy loading `foo` and using the original module *only* as a type annotation
  4. var _foo: typeof foo = require('foo');
  5. // Now use `_foo` as a variable instead of `foo`.
  6. }

A similar sample in amd (using requirejs) would be:

  1. import foo = require('foo');
  2. export function loadFoo() {
  3. // This is lazy loading `foo` and using the original module *only* as a type annotation
  4. require(['foo'], (_foo: typeof foo) => {
  5. // Now use `_foo` as a variable instead of `foo`.
  6. });
  7. }

This pattern is commonly used:

  • in web apps where you load certain JavaScript on particular routes,
  • in node applications where you only load certain modules if needed to speed up application bootup.

Use case: Breaking Circular dependencies

Similar to the lazy loading use case certain module loaders (commonjs/node and amd/requirejs) don’t work well with circular dependencies. In such cases it is useful to have lazy loading code in one direction and loading the modules upfront in the other direction.

Use case: Ensure Import

Sometimes you want to load a file just for the side effect (e.g. the module might register itself with some library like CodeMirror addons etc.). However if you just do a import/require the transpiled JavaScript will not contain a dependency on the module and your module loader (e.g. webpack) might completely ignore the import. In such cases you can use a ensureImport variable to ensure that the compiled JavaScript takes a dependency on the module e.g.:

  1. import foo = require('./foo');
  2. import bar = require('./bar');
  3. import bas = require('./bas');
  4. const ensureImport: any =
  5. foo
  6. || bar
  7. || bas;