https://github.com/iamolegga/collection-decorator
Decorator for adding to collection
https://github.com/iamolegga/collection-decorator
Last synced: 6 months ago
JSON representation
Decorator for adding to collection
- Host: GitHub
- URL: https://github.com/iamolegga/collection-decorator
- Owner: iamolegga
- License: mit
- Created: 2019-10-09T14:55:39.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T22:24:42.000Z (almost 3 years ago)
- Last Synced: 2025-03-10T19:42:28.809Z (7 months ago)
- Language: TypeScript
- Size: 268 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# collection-decorator
Decorator for adding to collection
[](https://www.npmjs.com/package/collection-decorator)
[](https://travis-ci.org/iamolegga/collection-decorator)
[](https://coveralls.io/github/iamolegga/collection-decorator?branch=master)
Typical usecase for this library:
```ts
// HasFoo.ts
import { createCollectionDecorator } from 'collection-decorator';interface ClassType {
foo: number;
}const { collection, decorator } =
createCollectionDecorator();export function fooSum() {
let sum = 0;
for (let value of collection.values()) {
sum += value.foo
}
return sum;
}export const HasFoo = decorator;
``````ts
// A.ts
import { HasFoo } from './HasFoo.ts';@HasFoo
export class A {
static foo = 1;
}
``````ts
// B.ts
import { HasFoo } from './HasFoo.ts';@HasFoo
//^^^^^ TS error because class B has not `foo` property
export class B {}// So fix it:
@HasFoo
export class B {
static foo = 2;
}
``````ts
// sum.ts
import { fooSum } from './HasFoo.ts';fooSum() // 3
```