https://github.com/writetome51/get-class-modification-decorator
Returns a TypeScript ClassDecorator factory.
https://github.com/writetome51/get-class-modification-decorator
class decorator factory function typescript
Last synced: 11 months ago
JSON representation
Returns a TypeScript ClassDecorator factory.
- Host: GitHub
- URL: https://github.com/writetome51/get-class-modification-decorator
- Owner: writetome51
- License: mit
- Created: 2019-11-13T02:24:47.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-08-09T01:51:29.000Z (almost 6 years ago)
- Last Synced: 2025-06-30T20:19:29.435Z (12 months ago)
- Topics: class, decorator, factory, function, typescript
- Language: TypeScript
- Homepage:
- Size: 33.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# getClassModificationDecorator(
modifyInstance: (
instance, decoratorArgs: any[]
) => void
): (...decoratorArgs) => ClassDecorator
Returns a TypeScript ClassDecorator factory. You set the decorator behavior with the
callback `modifyInstance()`. The decorator creates a new constructor for the class
being decorated. The new constructor first calls the original, then the class instance
is passed into `modifyInstance()`, where you manipulate it however you want. (The
prototype chain is kept intact, and the `instanceof` operator will still work.)
## Examples
```ts
// Here we make a decorator that adds properties:
export const add_properties = getClassModificationDecorator(
(instance, decoratorArgs: [object]) => {
let newProperties = decoratorArgs[0];
Object.assign(instance, newProperties);
}
);
// Now use it:
@add_properties({hair: 'amazing', age: 50, income: 200000, wife: 'hot'})
export class Boss {
}
@add_properties({address: '100 fleet street', income: 600000})
export class CEO extends Boss {
}
let ceo = new CEO();
console.log(ceo);
/*************
CEO {
age: 50,
address: '100 fleet street',
hair: 'amazing',
income: 600000,
wife: 'hot'
}
**************/
```
## Installation
```bash
npm i @writetome51/get-class-modification-decorator
```
## Loading
```js
import {getClassModificationDecorator}
from '@writetome51/get-class-modification-decorator';
```