javascripttypescriptwebpackbabeljsbabel-preset-env

Babel with preset-env (no options) and preset-typescript says "Missing class properties transform". Why?


I've got a fairly simple Babel config:

            {
              presets: [
                [
                  '@babel/preset-typescript',
                  {
                    isTSX: true,
                    allExtensions: true,
                    allowNamespaces: true,
                    onlyRemoveTypeImports: true,
                  },
                ],
                ['@babel/preset-env', {}],
              ],
              plugins: ['@babel/plugin-transform-react-jsx'],
            },

This is being passed to babel-loader in my Webpack setup. Then I get errors like the following when I build:

ERROR in ./store/state/AppState.ts
Module build failed (from ../node_modules/babel-loader/lib/index.js):
SyntaxError: /home/trusktr/src/my-project/src/store/state/AppState.ts: Missing class properties transform.
  25 | 
  26 | export class AppState implements State<string> {
> 27 |   static Key = 'AppState'

Here's the versions:

❯ npm ls @babel/core @babel/preset-env @babel/preset-typescript @babel/plugin-transform-react-jsx babel-loader
my-project@1.0.0 /home/trusktr/src/my-project
├── @babel/core@7.9.0 
├── @babel/plugin-transform-react-jsx@7.9.4 
├── @babel/preset-env@7.9.5 
├── @babel/preset-typescript@7.9.0 
└── babel-loader@8.1.0

Any ideas why this happens? Isn't preset-env suppose to handle basic things like class fields?


Solution

  • You need to add "@babel/plugin-proposal-class-properties" to your plugins configuration. AFAIK, Babel presets does not do it by themselves.