Babel
  • Docs
  • Setup
  • Try it out
  • Videos
  • Blog
  • Donate
  • Team
  • GitHub

›All Blog Posts

All Blog Posts

  • 7.13.0 Released: Records and Tuples, granular compiler assumptions, and top-level targets
  • 7.12.0 Released: TypeScript 4.1, strings as import/export names, and class static blocks
  • 7.11.0 Released: ECMAScript 2021 support in preset-env, TypeScript 4.0 support, printing config and the future of `babel-eslint`
  • The State of babel-eslint
  • 7.10.0 Released: Class Fields in preset-env, '#private in' checks and better React tree-shaking
  • 7.9.0 Released: Smaller preset-env output, Typescript 3.8 support and a new JSX transform
  • 7.8.0 Released: ECMAScript 2020, .mjs configuration files and @babel/cli improvements
  • Babel's Funding Plans
  • 7.7.0 Released: Error recovery and TypeScript 3.7
  • 7.6.0 Released: Private static accessors and V8 intrinsic syntax
  • 7.5.0 Released: dynamic import and F# pipelines
  • The Babel Podcast
  • 7.4.0 Released: core-js 3, static private methods and partial application
  • 7.3.0 Released: Named capturing groups, private instance accessors and smart pipelines
  • 7.2.0 Released: Private Instance Methods
  • TC39 Standards Track Decorators in Babel
  • 7.1.0 Released: Decorators, Private Static Fields
  • Babel 7 Released
  • Removing Babel's Stage Presets
  • What's Happening With the Pipeline (|>) Proposal?
  • Announcing Babel's New Partnership with trivago!
  • On Consuming (and Publishing) ES2015+ Packages
  • Nearing the 7.0 Release
  • Babel Turns Three
  • Planning for 7.0
  • Zero-config code transformation with babel-plugin-macros
  • Contributing to Babel: Three Lessons to Remember
  • Personal Experiences at Babel #1 — A PR with Unusually High Number of Reviews
  • Babel and Summer of Code 2017
  • Upgrade to Babel 7 (moved)
  • Upgrade to Babel 7 for Tool Authors (WIP)
  • 6.23.0 Released
  • The State of Babel
  • 6.19.0 Released
  • 6.18.0 Released
  • 6.16.0 Released
  • Babili (babel-minify)
  • 6.14.0 Released
  • Babel Doctor
  • Setting up Babel 6
  • 6.0.0 Released
  • React on ES6+
  • Function Bind Syntax
  • 5.0.0 Released
  • Babel <3 React
  • Not Born to Die
  • 2to3
  • 6to5 + esnext

7.12.0 Released: TypeScript 4.1, strings as import/export names, and class static blocks

October 15, 2020

Nicolò Ribaudo

We just published a new minor version of Babel!

This release includes support for the new TypeScript 4.1 beta features: template literal types and key remapping in mapped types.

Additionally, we implemented two new ECMAScript proposals: class static blocks and imports and exports with string names.

Lastly, we renamed @babel/plugin-syntax-module-attributes (and the corresponding parser plugin moduleAttributes) to @babel/plugin-syntax-import-assertions (and importAssertions), to match the recent proposal updates. The old plugin will work until Babel 8, but it's deprecated now.

You can read the whole changelog on GitHub.

If you or your company want to support Babel and the evolution of JavaScript, but aren't sure how, you can donate to us on our Open Collective and, better yet, work with us on the implementation of new ECMAScript proposals directly! As a volunteer-driven project, we rely on the community's support to fund our efforts in supporting the wide range of JavaScript users. Reach out at team@babeljs.io if you'd like to discuss more!

Highlights

TypeScript 4.1 (#12129, #12131)

TypeScript 4.1 beta was announced a few weeks ago, and it includes new syntax features for types.

Template literal types allow concatenating strings at the type-level, using the template literal syntax:

type Events = "Click" | "Focus";

type Handler = {
  [K in `on${Events}`]: Function
};

const handler: Handler = {
    onClick() {}, // Ok
    onFocus() {}, // Ok
    onHover() {}, // Error!
};

Together with key remapping in mapped types, they can be used to represent complex object transformations:

type Getters<T> = {
    [K in keyof T as `get${Capitalize<K>}`]: () => T[K]
};

interface Dog { name: string; age: number; }

const lazyDog: Getters<Dog> = /* ... */;
lazyDog.getName(); // string
lazyDog.age; // error!

You can read more about TypeScript 4.1 in the release announcement, or check other examples of what capabilities these new features unlock. However, remember that TypeScript 4.1 is still experimental!

Class static blocks (#12079, #12143)

Example on Babel REPL

class C {
  static #x = 42;
  static y;
  static {
    try {
      this.y = doSomethingWith(this.#x);
    } catch {
      this.y = "unknown";
    }
  }
}

This stage 2 proposal allows you to apply additional static initializations when a class definition is evaluated. It is not intended to replace static fields but to enable new use cases that could not be accomplished before. In the example above, the static property y is initialized using #x. If doSomethingWith(this.#x) throws, y will be assigned by a default value "unknown".

You can read more about it in the proposal's description.

Thanks to Jùnliàng, you can test this proposal by installing the @babel/plugin-proposal-class-static-block plugin and adding it to your Babel config. Since it is likely you're already using other class feature plugins, be sure to place this plugin before the others:

{
  "plugins": [
    "@babel/plugin-proposal-class-static-block",
    "@babel/plugin-proposal-class-properties"
  ]
}

ℹ️ Babel implements an updated version of spec, which addresses feedback we have provided based on the current spec.

Imports and exports with string names (#12091)

Consensus was achieved during the last TC39 meeting with a PR to allow strings as the name of imported and exported variables:

// emojis.js
let happy = "wooo!";
export { happy as "😃" };

// main.js
import { "😃" as smile } from "./emojis.js";
console.log(smile); // wooo!

This allows using any valid UTF-16 name across modules, making JavaScript fully compatible with other languages such as WebAssembly.

You can enable parsing support for this feature by adding @babel/plugin-syntax-module-string-names to your configuration:

// babel.config.json
{
  "presets:" ["@babel/preset-env"],
  "plugins": [
    "@babel/syntax-module-string-names"
  ]
}

This feature will be enabled by default as soon as the syntax is merged into the main ECMAScript specification.

Please note that it's not possible to transpile arbitrary strings to ES2015-style imports and exports: they will only be transpiled when targeting a different modules system such as CommonJS.

Import assertions parsing (#12139)

The "module attributes" proposal has been significantly changed and also renamed to "import assertions".

We've implemented parsing support for this new version of the proposal, which can be enabled using the @babel/plugin-syntax-import-assertions plugin (or, if you are directly using @babel/parser, importAssertions):

 {
   "plugins": [
-    ["@babel/syntax-module-attributes", { "version": "may-2020" }]
+    "@babel/syntax-import-assertions"
   ]
 }

The most significant syntax changes are that the with keyword has been replaced with assert and assertions are now wrapped in curly braces:

import json from "./foo.json" assert { type: "json" };

import("foo.json", { assert: { type: "json" } });

You can read more about these changes in the proposal's README.

⚠️ @babel/plugin-syntax-module-attributes will continue working until we release Babel 8.0.0, but will no longer be maintained, so we highly recommended migrating to the new plugin.

Recent Posts
  • Highlights
    • TypeScript 4.1 (#12129, #12131)
    • Class static blocks (#12079, #12143)
    • Imports and exports with string names (#12091)
  • Import assertions parsing (#12139)
Babel
Docs
Learn ES2015
Community
VideosUser ShowcaseStack OverflowSlack ChannelTwitter
More
BlogGitHub OrgGitHub RepoWebsite RepoOld 6.x SiteOld 5.x Site