TSConfig
importHelpers
For certain downleveling operations, TypeScript uses some helper code for operations like extending class, spreading arrays or objects, and async operations. By default, these helpers are inserted into files which use them. This can result in code duplication if the same helper is used in many different modules.
If the importHelpers
flag is on, these helper functions are instead imported from the tslib module.
You will need to ensure that the tslib
module is able to be imported at runtime.
This only affects modules; global script files will not attempt to import modules.
For example, with this TypeScript:
ts
export function fn(arr: number[]) {const arr2 = [1, ...arr];}
Turning on downlevelIteration
and importHelpers
is still false:
tsTry
var __read = (this && this.__read) || function (o, n) {var m = typeof Symbol === "function" && o[Symbol.iterator];if (!m) return o;var i = m.call(o), r, ar = [], e;try {while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);}catch (error) { e = { error: error }; }finally {try {if (r && !r.done && (m = i["return"])) m.call(i);}finally { if (e) throw e.error; }}return ar;};var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {if (ar || !(i in from)) {if (!ar) ar = Array.prototype.slice.call(from, 0, i);ar[i] = from[i];}}return to.concat(ar || Array.prototype.slice.call(from));};export function fn(arr) {var arr2 = __spreadArray([1], __read(arr), false);}
Then turning on both downlevelIteration
and importHelpers
:
tsTry
import { __read, __spreadArray } from "tslib";export function fn(arr) {var arr2 = __spreadArray([1], __read(arr), false);}
You can use noEmitHelpers
when you provide your own implementations of these functions.