Decorator factories

The preceding example is rigid because the options object will always have the same value. What if we needed to receive dynamic values? Of course, that's a valid question to ask because the id property may not always be #main. Therefore, we need to be more flexible.

Decorator factories are functions that return a decorator, giving you the power to pass in arguments for the decorator via its factory:

// decorator factory functionfunction config(options) {  // decorator function  return function(target) {    target.options = options  }}// class decorator// with arguments@config({id: '#main', show: true})class App {}// options addedconsole.log(App.options) // {id: '#main', show: true}

Get TypeScript 2.x for Angular Developers now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.