Support for external helpers library (tslib)

TypeScript injects a handful of helper functions such as extends for inheritance, assign for spread operator in object literals and JSX elements, and __awaiter for async functions.

Previously there were two options:

  • inject helpers in every file that needs them, or
  • no helpers at all with —noEmitHelpers.The two options left more to be desired;bundling the helpers in every file was a pain point for customers trying to keep their package size small.And not including helpers, meant customers had to maintain their own helpers library.

TypeScript 2.1 allows for including these files in your project once in a separate module, and the compiler will emit imports to them as needed.

First, install the tslib utility library:

  1. npm install tslib

Second, compile your files using —importHelpers:

  1. tsc --module commonjs --importHelpers a.ts

So given the following input, the resulting .js file will include an import to tslib and use the __assign helper from it instead of inlining it.

  1. export const o = { a: 1, name: "o" };
  2. export const copy = { ...o };
  1. "use strict";
  2. var tslib_1 = require("tslib");
  3. exports.o = { a: 1, name: "o" };
  4. exports.copy = tslib_1.__assign({}, exports.o);