collection-decorator
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i collection-decorator

Weekly Downloads

7

Version

0.1.1

License

MIT

Unpacked Size

6.61 kB

Total Files

8

Last publish

Collaborators

  • iamolegga