core-decorators
raw JSON →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).
Common errors
error Uncaught TypeError: Class constructor MyClass cannot be invoked without 'new' ↓
error Uncaught TypeError: Cannot assign to read only property 'PI' of object '#<MyClass>' ↓
error Decorators are not supported in this environment (e.g., 'Decorators are not valid here' error in Babel) ↓
error TypeScript: 'experimentalDecorators' must be enabled to use decorators. ↓
Warnings
breaking @debounce, @throttle, @memoize, @deprecated and other utility decorators are deprecated in core-decorators and will be removed in future versions. Use lodash-decorators instead. ↓
gotcha 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. ↓
gotcha 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). ↓
gotcha The @mixin decorator does not work with ES6 classes that extend from other classes; it only works on plain classes. ↓
deprecated @deprecated decorator has been deprecated in core-decorators itself. It still exists but may be removed. ↓
Install
npm install core-decorators yarn add core-decorators pnpm add core-decorators Imports
- default wrong
const coreDecorators = require('core-decorators')correctimport coreDecorators from 'core-decorators' - autobind wrong
import autobind from 'core-decorators/lib/autobind'correctimport { autobind } from 'core-decorators' - readonly wrong
const readonly = require('core-decorators').readonlycorrectimport { readonly } from 'core-decorators' - override
import { override } from 'core-decorators'
Quickstart
import { autobind, readonly, override, debounce, memoize } from 'core-decorators';
class MyClass {
@autobind
onClick() {
console.log('Clicked!');
}
@readonly
PI = 3.14159;
@override
toString() {
return `MyClass with PI=${this.PI}`;
}
@debounce(500)
saveInput() {
console.log('Saving...');
}
@memoize
expensiveOperation(n) {
// simulated slow function
return n * 2;
}
}
const instance = new MyClass();
instance.onClick();
console.log(instance.PI); // 3.14159
instance.PI = 3; // throws in strict mode
console.log(instance.toString()); // MyClass with PI=3.14159
// Note: @debounce and @memoize are deprecated in favor of lodash-decorators.