@babel/plugin-transform-typescript
Does not type-check its input. For that, you will need to install and set up TypeScript.
Caveats
Does not support
namespace
s.Workaround: Move to using file exports, or migrate to using the
module { }
syntax instead.Does not support
const enum
s because those require type information to compile.Workaround: Remove the
const
, which makes it available at runtime.Does not support
export =
andimport =
, because those cannot be compiled to ES.next.Workarounds:
- Use the plugin babel-plugin-replace-ts-export-assignment to transform
export =
. - Convert to using
export default
andexport const
, andimport x, {y} from "z"
.
- Use the plugin babel-plugin-replace-ts-export-assignment to transform
Behaves as if the
--isolatedModules
option was passed to the TypeScript Compiler. This can't be worked around because Babel doesn't support cross-file analysis.Does not load
tsconfig.json
files. Some options are supported in alternative ways: seeTypeScript Compiler Options
Example
In
const x: number = 0;
Out
const x = 0;
Installation
npm install --save-dev @babel/plugin-transform-typescript
Usage
With a configuration file (Recommended)
{
"plugins": ["@babel/plugin-transform-typescript"]
}
Via CLI
babel --plugins @babel/plugin-transform-typescript script.js
Via Node API
require("@babel/core").transform("code", {
plugins: ["@babel/plugin-transform-typescript"]
});
Options
isTSX
boolean
, defaults to false
Forcibly enables jsx
parsing. Otherwise angle brackets will be treated as typescript's legacy type assertion var foo = <string>bar;
. Also, isTSX: true
requires allExtensions: true
.
jsxPragma
string
, defaults to React
Replace the function used when compiling JSX expressions. This is so that we know that the import is not a type import, and should not be removed.
You can read more about configuring plugin options here
TypeScript Compiler Options
The official TypeScript compiler has many options which allow configuring both
the compiler and the type checker. While many of them don't make sense for Babel (for example,
--allowUnusedLabels
), some of them might be useful. Their behavior can be achieved in Babel
using different configuration options or plugins.
--alwaysStrict
You can use thestrictMode
parser option:module.exports = { parserOpts: { strictMode: true }, };
--downlevelIteration
You can use the@babel/plugin-transform-for-of
plugin. If you are using@babel/preset-env
,for...of
is already transpiled using iterators when it isn't supported by your compilation target.--emitDecoratorMetadata
This option isn't supported by an official Babel package since it is not part of the decorators proposal, but it is a TypeScript-specific addition. If you rely on this feature, you can use the community plugin babel-plugin-transform-typescript-metadata.--esModuleInterop
This is the default behavior of Babel when transpiling ECMAScript modules.--experimentalDecorators
This option enables support for the "legacy" decorator proposal. You can enable it in Babel using the@babel/plugin-proposal-decorators
plugin, although there are some minor differences.module.exports = { plugins: [ ["@babel/plugin-proposal-decorators", { legacy: true }] ] };
--importHelpers
This is the equivalent of the@babel/plugin-transform-runtime
package.--inlineSourceMap
You can set thesourceMaps: "inline"
option in yourbabel.config.js
file.--isolatedModules
This is the default Babel behavior, and it can't be turned off because Babel doesn't support cross-file analysis.--jsx
JSX support is provided using another plugin. If you want your output to contains JSX code (i.e.--jsx preserve
), you need the@babel/plugin-syntax-jsx
plugin; if you want to transpile it to standard JavaScript (i.e.--jsx react
or--jsx react-native
), you should use the@babel/plugin-transform-react-jsx
plugin.--jsxFactory
It can be customized using thepragma
option of the@babel/plugin-transform-react-jsx
package. You also need to set thejsxPragma
option of this plugin.--module
,-m
If you are using a bundler (Webpack or Rollup), this option is set automatically. If you are using@babel/preset-env
, you can use themodules
option; otherwise you can load the specific plugin.--module
value@babel/preset-env
'smodules
Single plugin None
false
/ CommonJS
"commonjs"
or"cjs"
@babel/plugin-transform-modules-commonjs
AMD
"amd"
@babel/plugin-transform-modules-amd
System
"systemjs"
@babel/plugin-transform-modules-systemjs
UMD
"umd"
@babel/plugin-transform-modules-umd
ES6
orES2015
false
/ --outDir
When using@babel/cli
, you can set the--out-dir
option.--outFile
Babel doesn't support concatenating output files: you should use a bundler (like Webpack, Rollup or Parcel) for that. When using@babel/cli
, you can compile a single file using the--out-file
option.--sourceMap
You can use the top-levelsourceMaps: true
option.--target
Babel doesn't support targeting a specific version of the language, but you can choose which engines you want to target using@babel/preset-env
. If you prefer, you can enable individual plugins for every ECMAScript feature.--watch
,-w
When using@babel/cli
, you can specify the--watch
option.