Skip to content
This repository has been archived by the owner on Sep 29, 2020. It is now read-only.

@autobind on classes doesn't work with Typescript #86

Open
bushmango opened this issue Sep 9, 2016 · 6 comments
Open

@autobind on classes doesn't work with Typescript #86

bushmango opened this issue Sep 9, 2016 · 6 comments

Comments

@bushmango
Copy link

SyntaxError: @autoBind can only be used on functions, not: undefined

@autoBind
class AGreatComponent extends React.Component<{},{}> {
...
}

My guess is there's a problem in the class vs function detection algorithm:

https://github.com/jayphelps/core-decorators.js/blob/master/src/autobind.js

function handle(args) {
if (args.length === 1) {
return autobindClass(...args);
} else {
return autobindMethod(...args); <-- code is going here instead
}
}

I'm not sure how to fix it.

I love core-decorators by the way! Made my last project much cleaner. Thanks for your work!

@bushmango
Copy link
Author

Upon further testing, it doesn't work when applied to functions either D:

SyntaxError: @autoBind can only be used on functions, not: undefined

class AGreatComponent extends React.Component<{},{}> {

@autoBind
_bound() {
logger.x('bound func called')
}
...

@jayphelps
Copy link
Owner

What version of TypeScript? What compiler flags?

@bushmango
Copy link
Author

"typescript": "^2.0.2" (RC)

"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"noImplicitAny": false,
"removeComments": false,
"preserveConstEnums": true,
"declaration": true,
"pretty": true,
"outDir": "./lib/",
"experimentalDecorators": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true,
"pretty": true,
"jsx": "react",
"baseUrl": "."
},

@Falieson
Copy link

Falieson commented Aug 2, 2017

I'm looking forward to movement on this

@0x80
Copy link

0x80 commented Nov 6, 2017

I use an arrow function instead. Seems to work just fine.

 private handleSelection = (record, index, event) => {
    this.setState(() => ({ selectedData: record }));
  };

@dewwwald
Copy link

dewwwald commented Apr 26, 2018

@0x80 Arrow function has its own set of problems.

  • New function is created every time you construct a class, so you take a performance hit. Think of constructing 100 + model classes, this becomes noticeable.
  • browser support, that method of binding causes issues in ie (or at least , that is what brought me here)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants