Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BindAll: Class constructor cannot be invoked without 'new' #47

Closed
icfantv opened this issue Jun 5, 2018 · 10 comments
Closed

BindAll: Class constructor cannot be invoked without 'new' #47

icfantv opened this issue Jun 5, 2018 · 10 comments
Assignees
Labels

Comments

@icfantv
Copy link

icfantv commented Jun 5, 2018

This is probably the same issue as #26 but I have a repo that reproduces the issue.

Repo: https://github.com/icfantv/lodash-decorator-issue
Just run yarn install followed by yarn test

If you run tsc you can see the generated output in the build directory. I freely admit I don't understand decorators and the generated code well enough to diagnose this as an issue w/ this lib or perhaps ts-jest. Happy to provide assistance if/where I can.

@icfantv
Copy link
Author

icfantv commented Jun 6, 2018

Digging further, it appears the decorator works fine in production which would seem to indicate where to look further. Going to keep digging.

@icfantv
Copy link
Author

icfantv commented Jun 6, 2018

Update from above reference: I can reproduce this with react-testing-library as well but am still not yet convinced it's an issue with this library.

@icfantv
Copy link
Author

icfantv commented Jun 6, 2018

Another update: I tried with core-decorators instead of lodash-decorators and the tests pass just fine. So now I'm back to square one and can't say for sure that there's not an issue w/ lodash-decorators.

@steelsojka
Copy link
Owner

This could be an issue with lodash-decorators implementation. core-decorators swaps out the method on the prototype with a getter that binds upon accessing the property on the instance. lodash-decorators tries to replace the constructor with one that binds upon construction (I don't think this works with all transpilers). I like the core-decorators approach to this. I'll try and find some time to create a similar implementation using lodash.

@icfantv
Copy link
Author

icfantv commented Jun 9, 2018

@steelsojka I have a concern about the core-decorators approach and I'm not convinced it has to be this way (but my JS-fu is not strong): It's going to execute that get() every time a bound method is executed which seems very non-performant (haven't tested this) and not a good design (maybe...).

Whereas, your solution is what React even recommends for <T extends React.Component> in that one should bind all non-static methods that access this (except for render() which React does automatically) in the constructor.

@steelsojka
Copy link
Owner

The problem with my approach is that we hi jack the constructor and invoke the original with .apply. This doesn't jive with some transpilers and produces the error you are getting. We won't call the getter everytime, only on the first access. In that getter we define the property on the instance bound. So I don't think there will be much change in behavior. I'll make sure to test these scenarios.

@steelsojka
Copy link
Owner

@icfantv So the error occurs when using native classes.

class MyClass {}
MyClass.apply({});

I'll try and find a way to make it work.

@steelsojka
Copy link
Owner

@icfantv I think I have a fix. I will try and get it out in the next day or so.

@icfantv
Copy link
Author

icfantv commented Jun 9, 2018

@steelsojka awesome and thanks! glad to hear it and happy to take a look.

@steelsojka
Copy link
Owner

Since this is a breaking change in behavior, this is in version 6.0.0.

@steelsojka steelsojka added the bug label Jun 17, 2018
@steelsojka steelsojka self-assigned this Jun 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants