// A WeakMap allows us to create a mapping where keys are class definitions. We still need to
// keep a separate array with a reference to each class, since we can't iterate over a WeakMap
const classDescription: WeakMap<Function, string> = new WeakMap()
const documentedClasses: Function[] = []
// The decorator function takes a string, but could take an object or any number of parameters
function documentation(description: string): ClassDecorator {
// Produce a class decorator function
return function<Cls extends Function>(target: Cls): Cls {
classDescription.set(target, description)
documentedClasses.push(target)
return target
}
}
// Use the decorator on multiple classes
@documentation('A virtual shopping cart')
class ShoppingCart { /* ... */ }
@documentation('Items that have been paid for')
class Purchase { /* ... */ }
for (const key of documentedClasses) {
console.log(key.name + ': ' + classDescription.get(key))
}