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.51k stars 263 forks source link

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

Open bushmango opened 8 years ago

bushmango commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

What version of TypeScript? What compiler flags?

bushmango commented 8 years ago

"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 commented 7 years ago

I'm looking forward to movement on this

0x80 commented 7 years ago

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

 private handleSelection = (record, index, event) => {
    this.setState(() => ({ selectedData: record }));
  };
dewwwald commented 6 years ago

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