Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. function Init(obj: { [key: string]: any }, Class: any) {
  2. return function(target: any, name: string) {
  3. Object.defineProperty(target, name, {
  4. get: () => {
  5. return new Class(obj[name]);
  6. },
  7. });
  8. };
  9. }
  10.  
  11. class Item {
  12. item: IItem;
  13. associatedItems: Item[];
  14.  
  15. constructor(item: IItem, ...associatedItems: Item[]) {
  16. this.item = item;
  17. this.associatedItems = associatedItems;
  18. }
  19.  
  20. doSomething() {
  21. return true;
  22. }
  23. }
  24.  
  25. interface IItem {
  26. some: string;
  27. }
  28.  
  29. const items: { [K in keyof Items]: IItem } = {
  30. AIR_PODS: {
  31. some: 'key',
  32. },
  33. BOSE_SPEAKER: {
  34. some: 'thiing',
  35. },
  36. };
  37.  
  38. class Items {
  39. @Init(items, Item)
  40. AIR_PODS: Item;
  41. BOSE_SPEAKER: Item;
  42. }
  43.  
  44. const thing = new Items();
  45.  
  46. console.log(thing.AIR_PODS.doSomething());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement