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.

wrong js code when using ts constructor assignment

See original GitHub issue

Bug Report

  • I would like to work on a fix!

Current Behavior If a module imported from TypeScript has the same name as a constructor parameter, a bug occurs.

Input Code

import { messaging } from 'firebase-admin'

export class Something {
  constructor(
  	public messaging: messaging.Messaging
  ) {
  }
}

Expected behavior/code

Expected,

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Something = void 0;

class Something {
  constructor(messaging) {
    this.messaging = messaging;
  }

}

exports.Something = Something;

But actual,

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Something = void 0;

var _firebaseAdmin = require("firebase-admin");

class Something {
  constructor(_firebaseAdmin.messaging) { // Whoops!
    this.messaging = messaging;
  }

}

exports.Something = Something;

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

  • Filename: .babelrc
{
  "presets": [
    ["@babel/env", {
      "targets": "maintained node versions",
      "useBuiltIns": "usage",
      "corejs": 3
    }],
    "@babel/typescript"
  ]
}

Environment

  System:
    OS: macOS 10.15.2
  Binaries:
    Node: 10.16.3 - ~/.nvm/versions/node/v10.16.3/bin/node
    Yarn: 1.17.3 - /usr/local/bin/yarn
    npm: 6.9.0 - ~/.nvm/versions/node/v10.16.3/bin/npm
  npmPackages:
    @babel/cli: ^7.8.3 => 7.8.3
    @babel/core: ^7.8.3 => 7.8.3
    @babel/plugin-proposal-class-properties: ^7.8.3 => 7.8.3
    @babel/plugin-proposal-decorators: ^7.8.3 => 7.8.3
    @babel/plugin-proposal-object-rest-spread: ^7.8.3 => 7.8.3
    @babel/plugin-proposal-optional-chaining: ^7.8.3 => 7.8.3
    @babel/preset-env: ^7.8.3 => 7.8.3
    @babel/preset-typescript: ^7.8.3 => 7.8.3
    eslint: ^6.2.2 => 6.2.2
    jest: ^24.9.0 => 24.9.0
  • Babel version(s): 7.8.3
  • Node/npm version: node 10.16.3 / npm 6.9.0
  • OS: macOS 10.15.2
  • Monorepo: no
  • How you are using Babel: cli

Possible Solution

Additional context/Screenshots .

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
sidntrivedi012commented, Apr 23, 2020

Sorry for delaying the work on this issue. Will start working on it from today onwards.

2reactions
dangreavescommented, Apr 3, 2020

I experienced a very similar issue to this when importing a type with the same name as the constructor argument. However, in my case, the output was slightly different to the above, resulting in a module import.

Source

import { S3EventRecord } from "aws-lambda";

export class SapFileCreated {
  constructor(protected S3EventRecord: S3EventRecord) {}
}

Expected output

class SapFileCreated {
  constructor(S3EventRecord) {
    this.S3EventRecord = S3EventRecord;
  }
}

Actual output

var _awsLambda = __webpack_require__(246);

class SapFileCreated {
  constructor(S3EventRecord) { // S3EventRecord is ignored here
    this.S3EventRecord = _awsLambda.S3EventRecord;
  }
}

Versions

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using TypeScript, and Object.assign gives me an error ...
javascript - Using TypeScript, and Object. assign gives me an error "property 'assign' does not exist on type 'ObjectConstructor'" - Stack ...
Read more >
Incorrect transpiled constructor of derived classes #12123
Code. class C extends Error { constructor() { super('error'); } ... However, if TS users incorrectly use primitive values, the current code ......
Read more >
TypeScript errors and how to fix them
A list of common TypeScript errors and how to fix them.
Read more >
Custom errors, extending Error
Here's the code with MyError and other custom error classes, simplified: class MyError extends Error { constructor(message) { super(message) ...
Read more >
TypeScript Done Wrong
Now that code won't work, because TS will not like you assigning "123" to your property. Problem solved! Private class attributes through the ......
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