Import Helpers - 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:
export function fn(arr: number[]) {
const arr2 = [1, ...arr];
}
Turning on downlevelIteration and importHelpers is still false:
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);
}
Try
Then turning on both downlevelIteration and importHelpers:
import { __read, __spreadArray } from "tslib";
export function fn(arr) {
var arr2 = __spreadArray([1], __read(arr), false);
}
Try
You can use noEmitHelpers when you provide your own implementations of these functions.
Related:
noEmitHelpers
downlevelIteration
Imports Not Used As Values - importsNotUsedAsValues
Deprecated in favor of verbatimModuleSyntax.
This flag controls how import works, there are 3 different options:
remove: The default behavior of dropping import statements which only reference types.
preserve: Preserves all import statements whose values or types are never used. This can cause imports/side-effects to be preserved.
error: This preserves all imports (the same as the preserve option), but will error when a value import is only used as a type. This might be useful if you want to ensure no values are being accidentally imported, but still make side-effect imports explicit.
This flag works because you can use import type to explicitly create an import statement which should never be emitted into JavaScript.
Default:
remove
Allowed:
remove
preserve
error
Related:
preserveValueImports
verbatimModuleSyntax
Released:
3.8
网友评论