question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript: Class parameter properties aren't compiled correctly when transformed after preset-env

See original GitHub issue

Bug Report

Current behavior When @babel/preset-typescript is placed before @babel/preset-env in the presets array and @babel/preset-env is configured to transform classes (by setting target to include ie 11), class parameter properties are not transpiled which will cause runtime exceptions.

Input Code

class Foo {
  constructor(public bar: number) {}
}

compiles to:

"use strict";

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Foo = function Foo(bar) {
  _classCallCheck(this, Foo);
};

.babelrc.json

{
  "presets": [
    "@babel/typescript",
    ["@babel/env", {
      "targets": "ie 11"
    }]
  ]
}

The same result can be produced by using the @babel/transform-classes plugin directly:

$ babel --no-babelrc --plugins @babel/transform-classes,@babel/transform-typescript test.ts

Expected behavior With the preset order reversed, it successfully compiles down to:

"use strict";

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Foo = function Foo(bar) {
  _classCallCheck(this, Foo);

  this.bar = bar;
};

Possible Solution This seems to be a limitation of plugin ordering where the class is compiled down to a function by preset-env before typescript can properly transform the parameter properties and does not look like it can easily be fixed. I suggest documenting this plugin ordering issue under the typescript preset/plugin docs so that people are aware of it since babel/preset-env is a very common preset that people will be using with typescript.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
dilyanpalauzovcommented, Sep 29, 2020
2reactions
JLHwungcommented, Nov 18, 2021

https://babeljs.io/docs/en/presets#preset-ordering

The preset is (unintuitively) applied from last to first. It was for backward compatibility back to 2015 when people use preset-es2015 and preset-latest in this order: ["preset-es2015", "preset-latest"]

It is correct that typescript preset should be applied before other ES language features, so preset-typescript should be placed after preset-env. I will update the preset-typescript and preset-flows about the ordering issues.

Why can’t babel warn or do something that doesn’t output broken code?

It is hard for Babel to warn about a certain preset order because Babel can’t statically know the usage of preset, for example:

presets: ["es-features-preset", "preset-typescript", "some-preset-output-to-typescript"]

We could warn for the builtin preset-env and preset-typescript, but we couldn’t do it in a general way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript parameter properties not working with Storybook + ...
This issue is caused by a known bug in Storybook. As a workaround, add the following code snippet to .storybook/main.js :
Read more >
babel/plugin-transform-typescript
Since Babel does not type-check, code which is syntactically correct, but would fail the TypeScript type-checking may successfully get transformed, and often in ......
Read more >
How To Use Classes in TypeScript | DigitalOcean
Classes are a common abstraction used in object-oriented programming (OOP) languages to describe data structures known as objects.
Read more >
API - ESBuild
You may also find the TypeScript type definitions for esbuild helpful as a ... code evaluation) are not bundled, since bundling is a...
Read more >
CoffeeScript
Compile a .coffee script into a .js JavaScript file of the same name. -t, --transpile, Pipe the CoffeeScript compiler's ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found