@babel/preset-typescript

    In

    Out

    1. const x = 0;
    1. npm install --save-dev @babel/preset-typescript

    Via CLI

    1. babel --presets @babel/preset-typescript script.ts

    Via Node API

    1. require("@babel/core").transformSync("code", {
    2. presets: ["@babel/preset-typescript"],
    3. });

    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.

    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.

    jsxPragmaFrag

    string, defaults to React.Fragment

    Replace the function used when compiling JSX fragment expressions. This is so that we know that the import is not a type import, and should not be removed.

    Indicates that every file should be parsed as TS, TSX, or as TS without JSX ambiguities (depending on the isTSX and disallowAmbiguousJSXLike options).

    allowNamespaces

    boolean, uses the default set by .

    Added in: v7.6.0

    Enables compilation of TypeScript namespaces.

    boolean, defaults to false

    Added in: v7.7.0

    When enabled, type-only class fields are only removed if they are prefixed with the declare modifier:

    disallowAmbiguousJSXLike

    boolean, defaults to true for .mts and .cts files and to false otherwise.

    Even when JSX parsing is not enabled, this option disallows using syntax that would be ambiguous with JSX (<X> y type assertions and <X>() => {} type arguments). It matches the tsc behavior when parsing .mts and .mjs files.

    onlyRemoveTypeImports

    boolean, defaults to false

    Added in: v7.9.0

    When set to true, the transform will only remove type-only imports (introduced in TypeScript 3.8). This should only be used if you are using TypeScript >= 3.8.

    optimizeConstEnums

    boolean, defaults to

    Added in: v7.15.0

    When set to true, Babel will inline enum values rather than using the usual enum output:

    1. // Input
    2. const enum Animals {
    3. Fish
    4. }
    5. console.log(Animals.Fish);
    6. // Default output
    7. var Animals;
    8. (function (Animals) {
    9. Animals[Animals["Fish"] = 0] = "Fish";
    10. })(Animals || (Animals = {}));
    11. console.log(Animals.Fish);
    12. // `optimizeConstEnums` output
    13. console.log(0);

    This option differs from TypeScript’s --isolatedModules behavior, which ignores the const modifier and compiles them as normal enums, and aligns Babel’s behavior with TypeScript’s default behavior.

    However, when exporting a const enum Babel will compile it to a plain object literal so that it doesn’t need to rely on cross-file analysis when compiling it:

    1. // Input
    2. export const enum Animals {
    3. Fish,
    4. }
    5. // `optimizeConstEnums` output
    6. export var Animals = {