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.

[@babel/plugin-transform-typescript] SyntaxError: Unknown PrivateName

See original GitHub issue

Bug Report

  • I would like to work on a fix! – But I don’t even know where to start from…

Search terms sharp field non-null assertion declarative sharp sharp non null

Current behavior

/repl.jsx: Unknown PrivateName "#field"
  3 | 
  4 |   myMethod() {
> 5 |     this.#field = new Date();
    |          ^^^^^^
  6 |     console.log(this.#field);
  7 |   }
  8 | }

Input Code

class Demo {
  #field!: Date;

  myMethod() {
    this.#field = new Date();
    console.log(this.#field);
  }
}

const demo = new Demo();
demo.myMethod();

Expected behavior

tsc compiles fine, so I would expect that babel could translate the code to javascript too, but it fails.

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: babel.config.js
module.exports = {
    plugins: [
        '@babel/plugin-transform-typescript',
        '@babel/plugin-proposal-class-properties',
        '@babel/plugin-proposal-json-strings',
        '@babel/plugin-proposal-object-rest-spread',
        ['transform-remove-console', { exclude: [ 'error', 'warn'] }],
        './babel-plugins/version.js'
    ],
    presets: [
        [
            '@babel/preset-env',
            {
                targets: {
                    browsers: ['> 0.25%, not dead']
                },
                modules: 'umd'
            }
        ]
    ],
    env: {
        development: {
        },
        prod: {
            presets: [
                ['minify', {
                    mangle: {
                        keepClassName: true
                    }
                }]
            ]
        },
        test: {
            plugins: [
                'istanbul'
            ]
        }
    },
    shouldPrintComment: (val) => /@license|@preserve|@version/i.test(val),
};

Environment

  System:
    OS: macOS 10.15.6
  Binaries:
    Node: 12.19.0 - ~/.nvm/versions/node/v12.19.0/bin/node
    npm: 6.14.8 - ~/.nvm/versions/node/v12.19.0/bin/npm
  npmPackages:
    @babel/cli: ^7.11.6 => 7.11.6 
    @babel/core: ^7.11.6 => 7.11.6 
    @babel/node: ^7.10.5 => 7.10.5 
    @babel/plugin-proposal-class-properties: ^7.10.4 => 7.10.4 
    @babel/plugin-proposal-json-strings: ^7.10.4 => 7.10.4 
    @babel/plugin-proposal-object-rest-spread: ^7.11.0 => 7.11.0 
    @babel/plugin-transform-modules-systemjs: ^7.10.5 => 7.10.5 
    @babel/plugin-transform-typescript: ^7.11.0 => 7.11.0 
    @babel/preset-env: ^7.11.5 => 7.11.5 
    babel-minify: ^0.5.1 => 0.5.1 
    babel-plugin-transform-remove-console: ^6.9.4 => 6.9.4 
    babel-preset-es2015: ^6.24.1 => 6.24.1 

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
JLHwungcommented, Oct 14, 2020

@miqmago Fixed in Babel 7.12.0.

1reaction
JLHwungcommented, Oct 14, 2020

Oh, I just realize that this issue has been fixed in #12149 but not yet released. It will be fixed in 7.12.0. allowDeclareFields: true is still required.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Static Class Features: Stage 3 · Issue #8052 · babel ... - GitHub
class A { #name="A"; constructor() { console.log(this.#name); } }. gives me. SyntaxError: [...] : Unknown PrivateName "#name".
Read more >
@babel/plugin-proposal-class-properties - Package Manager
Intro. Babel is a tool that helps you write code in the latest version of JavaScript. When your supported environments don't support certain...
Read more >
Open Source Used In Intersight Mobile App 1.0.233 - Cisco
This document contains licenses and notices for open source software used in this product.
Read more >
TypeScript 'declare' fields must first be transformed by @babel ...
When trying to build my react app, I get a TypeScript 'declare' fields must first be transformed by @babel/plugin-transform-typescript. error.
Read more >
@babel/plugin-transform-typescript: Versions | Openbase
Full version history for @babel/plugin-transform-typescript including change logs. ... #14666 Support private name in decorator member expression (@JLHwung).
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