Babel Preset for TypeScript Proposals
The `babel-preset-proposal-typescript` (currently v4.0.0) is a Babel preset designed to enable experimental ECMAScript proposals in TypeScript projects that TypeScript itself does not yet natively support. This includes features like async do expressions, record and tuple, pipeline operator, and more, allowing developers to utilize cutting-edge syntax while still leveraging TypeScript's type-checking capabilities for standard features. The preset maintains an active development pace, with major versions typically aligned with updates to its core peer dependencies: `@babel/core` (currently `^7.23.0`) and `typescript` (currently `^5.3.0`). Its primary differentiator is its focused scope: it specifically fills the gap for proposals that the TypeScript team explicitly postpones or chooses not to implement, avoiding redundant transformations with `@babel/preset-typescript` or other standard presets.
Common errors
-
Cannot find module 'babel-preset-proposal-typescript'
cause The package is not installed or incorrectly referenced in `package.json`.fixEnsure `babel-preset-proposal-typescript` is listed in your `devDependencies` and installed: `npm install --save-dev babel-preset-proposal-typescript` or `yarn add -D babel-preset-proposal-typescript`. -
Requires Babel '^7.23.0'
cause Your installed `@babel/core` version does not meet the peer dependency requirement of `babel-preset-proposal-typescript@4.0.0`.fixUpgrade `@babel/core` to the required version: `npm install --save-dev @babel/core@^7.23.0` or `yarn add -D @babel/core@^7.23.0`. -
Requires TypeScript '^5.3.0'
cause Your installed `typescript` version does not meet the peer dependency requirement of `babel-preset-proposal-typescript@4.0.0`.fixUpgrade `typescript` to the required version: `npm install --save-dev typescript@^5.3.0` or `yarn add -D typescript@^5.3.0`. -
Error: .babelrc configuration is invalid. presets[2] must be a string
cause Incorrect format for the preset entry in `.babelrc` or `babel.config.js`.fixEnsure the preset is listed as a simple string `"babel-preset-proposal-typescript"` or an array `["babel-preset-proposal-typescript", { /* options */ }]` if you need to pass options.
Warnings
- breaking Version 4.0.0 requires `@babel/core@^7.23.0` and `typescript@^5.3.0`. Users on older versions of Babel or TypeScript will need to upgrade their peer dependencies.
- breaking Version 3.0.0 upgraded the minimum `typescript` peer dependency to `v4.7` and removed some internal plugins. This might cause compatibility issues for projects still using older TypeScript versions.
- breaking Version 2.0.0 introduced breaking changes by bumping both Babel and TypeScript minimum versions and adding new proposals. Specific details might vary, but expect potential configuration and dependency conflicts.
- gotcha Since `v1.5.0`, `nullish-coalescing-operator` and `optional-chaining` transformations were dropped as TypeScript 3.7.0 introduced native support for these features. Relying on this preset for these specific transformations will no longer work.
- gotcha This preset only transforms proposals which TypeScript does not *currently* support. As TypeScript evolves and adopts new ECMAScript proposals, this preset may remove plugins for those features. This could lead to unexpected behavior if your build relies on the preset transforming a feature that TypeScript has since adopted.
Install
-
npm install babel-preset-proposal-typescript -
yarn add babel-preset-proposal-typescript -
pnpm add babel-preset-proposal-typescript
Imports
- Babel Preset Configuration
import preset from 'babel-preset-proposal-typescript';
{ "presets": ["babel-preset-proposal-typescript"] } - Babel Node API Usage
import { PresetName } from 'babel-preset-proposal-typescript';require('babel-preset-proposal-typescript')
Quickstart
{
"plugins": [
// Ensure decorators are enabled if you use them, and are correctly configured.
// Babel's order matters: proposals before official features.
],
"presets": [
["@babel/preset-env", { "targets": { "node": "current" } }],
"@babel/preset-typescript",
"babel-preset-proposal-typescript"
]
}
// Example index.ts
async function runDoExpression() {
const result = await do {
let x = 10;
if (x > 5) {
await Promise.resolve(x * 2);
} else {
x * 3;
}
};
console.log('Do expression result:', result);
}
runDoExpression();
const tuple: [string, number] = #['hello', 123];
console.log('Tuple element:', tuple[0]);
function pipelineExample(value: number) {
return value
|> ((x) => x * 2)
|> ((x) => x + 1);
}
console.log('Pipeline result:', pipelineExample(5));
// package.json script
// "scripts": {
// "build": "babel src/index.ts --out-dir dist --extensions \".ts\""
// }