TypeScript Runtime Helper Library
tslib is a compact runtime library provided by Microsoft that contains all the essential helper functions emitted by the TypeScript compiler. It is primarily used in conjunction with the `--importHelpers` compiler option, which instructs TypeScript to import these common helper functions (such as `__extends`, `__assign`, `__decorate`, `__awaiter`) from `tslib` rather than embedding them directly in every compiled output file. This approach leads to significantly smaller JavaScript bundles and reduced runtime overhead by avoiding redundant function declarations. The current stable version is 2.8.1, with frequent patch and minor releases aligning with TypeScript's development. It differentiates itself by being the official, highly optimized runtime solution for TypeScript's emitted code, ensuring maximum compatibility and efficiency for projects compiled with TypeScript.
Common errors
-
Error: Cannot find module 'tslib' or its corresponding type declarations.
cause The `tslib` package is missing, or module resolution is failing to locate it when `importHelpers` is enabled in `tsconfig.json`.fixRun `npm install tslib` or `yarn add tslib`. Verify that `moduleResolution` in your `tsconfig.json` is correctly configured for your project (e.g., `node`, `node16`, `bundler`). -
TypeError: tslib_1.__assign is not a function
cause TypeScript's emitted code expects `tslib` helpers to be available at runtime, but `tslib` either was not installed, was incorrectly installed, or a bundler failed to include or correctly process it in the final output.fixEnsure `tslib` is installed (`npm install tslib`) and that your build pipeline (e.g., webpack, Rollup, esbuild) is configured to include and correctly bundle `tslib`'s runtime helpers. -
ReferenceError: __awaiter is not defined
cause This runtime error indicates that a `tslib` helper (such as `__awaiter` for `async/await`) is missing. This happens when `importHelpers` is enabled, but the `tslib` module is not loaded or its exports are inaccessible.fixConfirm `tslib` is a project dependency, is correctly installed, and that your module loader/bundler is successfully making the `tslib` helpers available at runtime. Ensure `tsconfig.json`'s `target` and `module` options are compatible with your runtime and `tslib` version.
Warnings
- breaking Older TypeScript versions require specific tslib versions. For TypeScript 3.8.4 or earlier, use `tslib@^1`. For TypeScript 2.3.2 or earlier, use `tslib@1.6.1`. Using a mismatch can lead to compilation errors or runtime issues where helpers are missing or incompatible.
- gotcha Projects using `moduleResolution: 'node16'` or `'bundler'` in `tsconfig.json` require `tslib@^2.5.1` or later due to changes in `package.json` exports field. Older versions might not resolve correctly, leading to 'Cannot find module' errors.
- gotcha New language features like `using` and `await using` declarations (introduced in TypeScript 5.2+) require `tslib@^2.6.0` or later. Using an older `tslib` with these features will result in runtime errors due to missing helper functions.
- gotcha The `__importStar` helper, used for `import * as name from 'module'`, did not correctly include non-enumerable keys from the imported module prior to v2.8.1. This could lead to missing properties at runtime for certain modules.
- deprecated The direct reference to `tslib.es6.js` from `package.json` exports was removed in `v2.5.3`. Projects relying on this specific path for ES6 output (e.g., custom bundler configurations) may experience broken imports.
Install
-
npm install tslib -
yarn add tslib -
pnpm add tslib
Imports
- __assign
const { __assign } = require('tslib');import { __assign } from 'tslib'; - __awaiter
import __awaiter from 'tslib';
import { __awaiter } from 'tslib'; - __decorate
const decorate = require('tslib').__decorate;import { __decorate } from 'tslib'; - __importStar
import * as tslib from 'tslib'; const myModule = tslib.__importStar(someObject);
import { __importStar } from 'tslib';
Quickstart
{
"compilerOptions": {
"importHelpers": true,
"target": "es2016",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}