4.7 Declaring ES6 modules

ES6 modules, also known as ECMAScript modules are the new syntax for the declaration of modules. The ES6 module declaration syntax is based in the ECMAScript standards. As today we can use ES6 modules at design time but someday they will become the native module definitions syntax for all JavaScript applications.

Getting Ready

All you need to implement in this recipe is an installation of TypeScript version 1.5 or higher.

How to do it

The library included in the companion source code declares a library called ndrscr. This library is implemented using ES6 modules under the following directory:


Declaring ES6 modules is almost identical to declaring external legacy modules:

import { IArrays } from '../../interfaces';

class Arrays implements IArrays {

  public first<T>(array: Array<T>, n = 1): Array<T> {
    const result = [];
    for (let i = 0; i < n; i++) {
      const value = array[i];
      if (typeof value === 'undefined') {
        return result;
    return result;

  public last<T>(array: Array<T>, n = 1): Array<T> {
    return this.first(array.reverse(), n);

export default new Arrays();

How it works

As you can see the ES6 exporting syntax is almost identical to the legacy external modules syntax and it is also used at design time. However, you should try to use the ES6 syntax rather than the legacy external modules syntax when possible because the ES6 syntax the only real proposed standard.

There’s more

You can declare multiple exports:

export arrays;
export arrays1;
export arrays2;

You can also use an in-line style:

export { arrays, arrays1, arrays2 };

It is also possible to flag one of the exported elements as the default export:

export default arrays;
export arrays1;
export arrays2;

Or using the in-line style:

export { default arrays, arrays1, arrays2 };

It is important to note that only one of them can be declared as the default export.

Source Code

Declaring ES6 modules

See also

Please refer to the preceding recipe to learn more about internal modules and legacy external modules.

Shiv Kushwaha