andreypopp / autobind-decorator

Decorator to automatically bind methods to class instances
MIT License
1.45k stars 66 forks source link

autobind doesn't work with typescript #50

Closed rohmanhm closed 7 years ago

rohmanhm commented 7 years ago
  @autobind
  handleReset () {
    this.setState({
      imgSrc: undefined
    });
  }

That code will throws an error in the browser console. This is the error message.

App.tsx:127 Uncaught TypeError: Cannot read property 'setState' of null at App.handleReset (http://localhost:3000/static/js/bundle.js:33337:14) at Object.ReactErrorUtils.invokeGuardedCallback (http://localhost:3000/static/js/bundle.js:17522:17) at executeDispatch (http://localhost:3000/static/js/bundle.js:17305:22) at Object.executeDispatchesInOrder (http://localhost:3000/static/js/bundle.js:17328:6) at executeDispatchesAndRelease (http://localhost:3000/static/js/bundle.js:16716:23) at executeDispatchesAndReleaseTopLevel (http://localhost:3000/static/js/bundle.js:16727:11) at Array.forEach (native) at forEachAccumulated (http://localhost:3000/static/js/bundle.js:17625:10) at Object.processEventQueue (http://localhost:3000/static/js/bundle.js:16930:8) at runEventQueueInBatch (http://localhost:3000/static/js/bundle.js:24552:19)

Here's my tsconfig.json configuration

{
  "compilerOptions": {
    "outDir": "build/dist",
    "module": "commonjs",
    "target": "es5",
    "lib": ["es6", "dom"],
    "sourceMap": true,
    "allowJs": true,
    "jsx": "react",
    "moduleResolution": "node",
    "rootDir": "src",
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    // "noImplicitAny": true,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": true,
    "experimentalDecorators": true
  },
  "exclude": [
    "node_modules",
    "build",
    "scripts",
    "acceptance-tests",
    "webpack",
    "jest",
    "src/setupTests.ts"
  ],
  "types": [
    "typePatches"
  ]
}
rohmanhm commented 7 years ago

You missing to publish this https://github.com/andreypopp/autobind-decorator/commit/a716cbdf238166925d4dcf26a1bb3f9fd269bbfb

That PR make works with Typescript

stevemao commented 7 years ago

published v1.4.1