{"id":25126,"library":"core-decorators","title":"core-decorators","description":"Library of JavaScript stage-0 decorators (ES2016/ES7 proposal) inspired by Java annotations, providing commonly used decorators like @override, @deprecate, @autobind, @mixin, @readonly, @nonconfigurable, @decorate, @extendDescriptor, @time, @deprecated, @suppressWarnings. Current stable version is 0.20.0. This library targets the original stage-0 decorator proposal (not the current stage-2 spec) and is intended for use with Babel 5 or Babel 6 with babel-plugin-transform-decorators-legacy. Key differentiator: lightweight, focused on fundamental JavaScript decorators, framework-agnostic (React, Angular, etc.). TypeScript definitions are included but there are known incompatibilities with TypeScript's decorator implementation. Similar to lodash-decorators but more minimal. Release cadence is low (last release 0.20.0 in 2020).","status":"maintenance","version":"0.20.0","language":"javascript","source_language":"en","source_url":"https://github.com/jayphelps/core-decorators.js","tags":["javascript","es6","es7","es2015","es2016","babel","decorators","override","deprecated","typescript"],"install":[{"cmd":"npm install core-decorators","lang":"bash","label":"npm"},{"cmd":"yarn add core-decorators","lang":"bash","label":"yarn"},{"cmd":"pnpm add core-decorators","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ESM default import works. CJS require also works but prefer ESM.","wrong":"const coreDecorators = require('core-decorators')","symbol":"default","correct":"import coreDecorators from 'core-decorators'"},{"note":"Named import from the main package is recommended. Avoid deep imports.","wrong":"import autobind from 'core-decorators/lib/autobind'","symbol":"autobind","correct":"import { autobind } from 'core-decorators'"},{"note":"Named import is supported. CJS require works but ESM is preferred.","wrong":"const readonly = require('core-decorators').readonly","symbol":"readonly","correct":"import { readonly } from 'core-decorators'"},{"note":"No common mistake for this decorator.","wrong":null,"symbol":"override","correct":"import { override } from 'core-decorators'"}],"quickstart":{"code":"import { autobind, readonly, override, debounce, memoize } from 'core-decorators';\n\nclass MyClass {\n  @autobind\n  onClick() {\n    console.log('Clicked!');\n  }\n\n  @readonly\n  PI = 3.14159;\n\n  @override\n  toString() {\n    return `MyClass with PI=${this.PI}`;\n  }\n\n  @debounce(500)\n  saveInput() {\n    console.log('Saving...');\n  }\n\n  @memoize\n  expensiveOperation(n) {\n    // simulated slow function\n    return n * 2;\n  }\n}\n\nconst instance = new MyClass();\ninstance.onClick();\nconsole.log(instance.PI); // 3.14159\ninstance.PI = 3; // throws in strict mode\nconsole.log(instance.toString()); // MyClass with PI=3.14159\n\n// Note: @debounce and @memoize are deprecated in favor of lodash-decorators.","lang":"typescript","description":"Shows usage of common decorators: autobind, readonly, override, debounce (deprecated), memoize (deprecated). Note deprecation warnings."},"warnings":[{"fix":"Replace @debounce with lodash-decorators' @debounce, etc.","message":"@debounce, @throttle, @memoize, @deprecated and other utility decorators are deprecated in core-decorators and will be removed in future versions. Use lodash-decorators instead.","severity":"breaking","affected_versions":">=0.20.0"},{"fix":"Ensure your transpiler is configured for stage-0 decorators (babel-plugin-transform-decorators-legacy for Babel 6, or Babel 5). For TypeScript, set experimentalDecorators: true.","message":"core-decorators only supports the stage-0 proposal, not the current stage-2 spec. Mixing with stage-2 decorators will cause incorrect behavior or errors.","severity":"gotcha","affected_versions":"all"},{"fix":"Consider using a different library (e.g., mobx-react or custom decorators) if you need TypeScript support. If you must use core-decorators, test thoroughly.","message":"TypeScript compatibility: core-decorators does NOT officially support TypeScript. There are known incompatibilities with the way TypeScript transpiles decorators (e.g., @autobind on methods may not work as expected).","severity":"gotcha","affected_versions":"all"},{"fix":"Do not use @mixin on classes that inherit from another class. Use composition or a different pattern.","message":"The @mixin decorator does not work with ES6 classes that extend from other classes; it only works on plain classes.","severity":"gotcha","affected_versions":"all"},{"fix":"Do not rely on @deprecated; implement your own deprecation warning logic.","message":"@deprecated decorator has been deprecated in core-decorators itself. It still exists but may be removed.","severity":"deprecated","affected_versions":">=0.20.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Ensure @autobind is applied to a method, not a class. For React class components, use 'handleClick = () => {}' or bind in constructor.","cause":"Using @autobind on a class method that is called as a callback (e.g., in React) without proper binding, or using the decorator incorrectly on a class property.","error":"Uncaught TypeError: Class constructor MyClass cannot be invoked without 'new'"},{"fix":"Do not attempt to reassign a @readonly decorated property. If you need mutable state, do not use @readonly.","cause":"Using @readonly on a property and then trying to assign a new value in strict mode.","error":"Uncaught TypeError: Cannot assign to read only property 'PI' of object '#<MyClass>'"},{"fix":"Add the appropriate Babel plugin. For Babel 7: npm install @babel/plugin-proposal-decorators --save-dev and add to .babelrc: { 'plugins': [['@babel/plugin-proposal-decorators', { 'legacy': true }]] }","cause":"Transpiler is not configured for stage-0 decorators. Babel 6 requires babel-plugin-transform-decorators-legacy; Babel 7 needs @babel/plugin-proposal-decorators with legacy: true.","error":"Decorators are not supported in this environment (e.g., 'Decorators are not valid here' error in Babel)"},{"fix":"Set 'experimentalDecorators': true in tsconfig.json. Note: core-decorators may still have TypeScript incompatibilities.","cause":"TypeScript compiler requires the experimentalDecorators option.","error":"TypeScript: 'experimentalDecorators' must be enabled to use decorators."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}