Skip to content

TypeError: Class constructor Person cannot be invoked without 'new' / _possibleConstructorReturn (T7328) #4269

Closed

Description

@babel-bot

Issue originally made by Benjamin Lupton (balupton)

Bug information

  • Babel version: 6.7.7
  • Node version: 6.0.0
  • npm version: 3.8.7

Options

--presets es2015

Input code

const Person = require('./native-class')
class Employee extends Person {}
const employee = new Employee()

Description

Babel fails to create code that is able to extend native classes (which could be published as an external module and consumed by our module)

Reproduction repo: https://github.com/balupton/babel-extend-native-class-issue

git clone https://github.com/balupton/babel-extend-native-class-issue.git
cd babel-extend-native-class-issue

npm install

node ./extend-native-class.js
babel --presets es2015 ./extend-native-class.js > ./extend-native-class-via-babel.js
node ./extend-native-class-via-babel.js

Error:

$ node ./extend-native-class-via-babel.js 
/Users/balupton/Projects/edition-stuff/babel-class-issue/extend-native-class-via-babel.js:17
    return _possibleConstructorReturn(this, Object.getPrototypeOf(Employee).apply(this, arguments));
                                                                            ^

TypeError: Class constructor Person cannot be invoked without 'new'
    at new Employee (/Users/balupton/Projects/edition-stuff/babel-class-issue/extend-native-class-via-babel.js:17:77)
    at Object.<anonymous> (/Users/balupton/Projects/edition-stuff/babel-class-issue/extend-native-class-via-babel.js:23:16)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:159:18)
    at node.js:444:3

Known affected versions:

"babel-cli": "6.7.7",
"babel-preset-es2015": "6.6.0"

typechecker includes tests for native classes - their code

Activity

  1. babel-bot commented on May 1, 2016

    @babel-bot
    CollaboratorAuthor

    Comment originally made by @loganfsmyth

    Babel fails to create code that is able to extend native classes

    Unfortunately that is much easier said than done. ES6 classes cannot be extended with ES5-style code. If your platform supports native classes, your best bet is to use a preset that is targeted to your platform, like babel-preset-es2015-node5, so then Babel will not transform the ES6 classes in your own code and they will work properly.

  2. babel-bot commented on May 1, 2016

    @babel-bot
    CollaboratorAuthor

    Comment originally made by Benjamin Lupton (balupton)

    Probably related to https://github.com/balupton/javascript-coffeescript-class-interop

    In which case, I'm not sure what the solution is.

  3. babel-bot commented on May 1, 2016

    @babel-bot
    CollaboratorAuthor

    Comment originally made by Benjamin Lupton (balupton)

    If your platform supports native classes, your best bet is to use a preset that is targeted to your platform, like babel-preset-es2015-node5, so then Babel will not transform the ES6 classes in your own code and they will work properly.

    Good suggestion, I'll give that a go!

  4. babel-bot commented on May 1, 2016

    @babel-bot
    CollaboratorAuthor

    Comment originally made by Benjamin Lupton (balupton)

    Okay, for my case, luckily the package I was consuming - https://github.com/bevry/fellow - uses https://github.com/bevry/editions - so I was able to change require('fellow') to require('fellow/es2015') which worked great.

    Happy to mark this as resolved, as my issue is now resolved, and doesn't seem possible for Babel to do anything here.

  5. babel-bot commented on Jul 16, 2016

    @babel-bot
    CollaboratorAuthor

    Comment originally made by @jdalton

    I just hit this issue while trying to simplify babel-plugin-lodash trying to extend a class with Map.
    Lodash avoids this issue by [[ https://github.com/lodash/lodash/blob/4.13.1-npm/_createCtorWrapper.js | using a switch statement ]] .

  6. babel-bot commented on Jul 16, 2016

    @babel-bot
    CollaboratorAuthor

    Comment originally made by @jdalton

    I've opened a PR – #3582

  7. added
    outdated
    on May 7, 2018
  8. locked as resolved and limited conversation to collaborators on May 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdated

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions