jayphelps / core-decorators

Library of stage-0 JavaScript decorators (aka ES2016/ES7 decorators but not accurate) inspired by languages that come with built-ins like @​override, @​deprecate, @​autobind, @​mixin and more. Popular with React/Angular, but is framework agnostic.
MIT License
4.52k stars 263 forks source link

autobind not working with async function #98

Open JuoCode opened 7 years ago

JuoCode commented 7 years ago

Use ES7 async function

@autobind
async handleSubmit() {
  this.setState({ loggedIn: true })
}

render() {
  return (
    <button onClick={this.handleSubmit}>Submit</button>
  )
}

Error message

Uncaught (in promise) TypeError: Cannot read property 'setState' of undefined
jayphelps commented 7 years ago

What compiler are you using?

JuoCode commented 7 years ago

Use tsc, and compile to target es6

jayphelps commented 7 years ago

What version of TypeScript? I need as much information as you can give me to help. Also keep in mind that core-decorators does not officially support TypeScript--there are known issues with it.

JuoCode commented 7 years ago
Typescript: 2.2.0-dev.20170120
core-decorators: 0.15.0
Node: 7.2.0

...core-decorators does not officially support TypeScript...

Working fine in general functions

damianobarbati commented 7 years ago

any news on this?

jayphelps commented 7 years ago

@damianobarbati No updates from me, but others are welcome to dig in and find the cause. 🤕

dantman commented 7 years ago

I use this pattern all the time in React Native (babel + browserify) so this error is definitely related to some interaction with TypeScript compiler.

davidsu commented 7 years ago

for me it works when running tests ( mocha + node + babel ) but fails on the browser ( webpack + babel )

BurtHarris commented 7 years ago

Could someone contribute a unit test for this? I'd like to include it in the typescript conversion effort. SEE PR #133.