collection-decorator
v0.1.1
Published
Decorator for adding to collection
Downloads
47
Readme
collection-decorator
Decorator for adding to collection
Typical usecase for this library:
// HasFoo.ts
import { createCollectionDecorator } from 'collection-decorator';
interface ClassType {
foo: number;
}
const { collection, decorator } =
createCollectionDecorator<ClassType>();
export function fooSum() {
let sum = 0;
for (let value of collection.values()) {
sum += value.foo
}
return sum;
}
export const HasFoo = decorator;
// A.ts
import { HasFoo } from './HasFoo.ts';
@HasFoo
export class A {
static foo = 1;
}
// 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;
}
// sum.ts
import { fooSum } from './HasFoo.ts';
fooSum() // 3