JavaScript doesn't have a built in language feature for defining abstract
classes and enforcing their contracts - but with decorators, we can create that
functionality ourselves! The @abstract
decorator allows you to define simple
abstract classes, like so:
@abstract
class CookieService {
@abstract headers;
@abstract getValue() {}
@abstract setValue() {}
}
You can then extend the class and provide the abstract values:
class BrowserCookieService extends CookieService {
headers = new Map();
getValue() {
// get cookie value
}
setValue(value) {
// set cookie value
}
}
If a subclass fails to provide the correct values, then an error will be thrown the first time the class is instatiated. The two types of abstract values that can be defined are fields and methods, and they are defined by decorating a class field or a method respectively.
The errors thrown by @abstract
are useful in development, but are pointless
overhead in production applications. If you're consuming this package, you can
remove the decorators using the filter-imports
babel plugin:
{
"plugins": [
[
"babel-plugin-filter-imports",
{
"imports": {
"abstract-decorator": ["default"]
}
}
]
]
}
This setup is provided out-of-the-box in Ember apps and addons, zero configuration necessary!
This package is compatible with Node v10 and above out of the box.
This package supports the stage 1/legacy decorators transform. This is transform that is the current recommendation of the champions of the decorators proposal.
This package does not support Typescript. Typescript has first class support for abstract classes, so there's no need to include extra weight via this decorator.
- Ember.js v2.18 or above
- Ember CLI v2.13 or above
- Node.js v8 or above
npm install --save abstract-decorator
# or with yarn
yarn add abstract-decorator
See the Contributing guide for details.
This project is licensed under the MIT License.