Registry / core-decorators

core-decorators

JSON →
library0.20.0jsnpmunverified

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).

npm install core-decorators
INSTALL
IMPORT
SIG · CORE-DECORATORS
C
core-decorators
javascriptv0.20.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import coreDecorators from 'core-decorators'
const coreDecorators = require('core-decorators')
ESM default import works. CJS require also works but prefer ESM.
autobind
import { autobind } from 'core-decorators'
import autobind from 'core-decorators/lib/autobind'
Named import from the main package is recommended. Avoid deep imports.
readonly
import { readonly } from 'core-decorators'
const readonly = require('core-decorators').readonly
Named import is supported. CJS require works but ESM is preferred.
override
import { override } from 'core-decorators'
No common mistake for this decorator.

Shows usage of common decorators: autobind, readonly, override, debounce (deprecated), memoize (deprecated). Note deprecation warnings.

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.
Debug
Known issues
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.
fix
Replace @debounce with lodash-decorators' @debounce, etc.
affects: >=0.20.0
gotchacore-decorators only supports the stage-0 proposal, not the current stage-2 spec. Mixing with stage-2 decorators will cause incorrect behavior or errors.
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.
affects: all
gotchaTypeScript 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).
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.
affects: all
gotchaThe @mixin decorator does not work with ES6 classes that extend from other classes; it only works on plain classes.
fix
Do not use @mixin on classes that inherit from another class. Use composition or a different pattern.
affects: all
deprecated@deprecated decorator has been deprecated in core-decorators itself. It still exists but may be removed.
fix
Do not rely on @deprecated; implement your own deprecation warning logic.
affects: >=0.20.0
Errors
Common errors & fixes
Uncaught TypeError: Class constructor MyClass cannot be invoked without 'new'
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.
fix
Ensure @autobind is applied to a method, not a class. For React class components, use 'handleClick = () => {}' or bind in constructor.
Uncaught TypeError: Cannot assign to read only property 'PI' of object '#<MyClass>'
Using @readonly on a property and then trying to assign a new value in strict mode.
fix
Do not attempt to reassign a @readonly decorated property. If you need mutable state, do not use @readonly.
Decorators are not supported in this environment (e.g., 'Decorators are not valid here' error in Babel)
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.
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 }]] }
TypeScript: 'experimentalDecorators' must be enabled to use decorators.
TypeScript compiler requires the experimentalDecorators option.
fix
Set 'experimentalDecorators': true in tsconfig.json. Note: core-decorators may still have TypeScript incompatibilities.
Upgrade
Version history
0.20.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
core-decorators — npm install core-decorators · libregistry