0.1.1 • Published 5 years ago

collection-decorator v0.1.1

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

collection-decorator

Decorator for adding to collection

npm Build status Coverage Status David

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