Explain how and why we could use property decorators in TS?

Technology CommunityCategory: TypeScriptExplain how and why we could use property decorators in TS?
VietMX Staff asked 3 years ago

Decorators can be used to modify the behavior of a class or become even more powerful when integrated into a framework. For instance, if your framework has methods with restricted access requirements (just for admin), it would be easy to write an @admin method decorator to deny access to non-administrative users, or an @owner decorator to only allow the owner of an object the ability to modify it.

class CRUD {
    get() { }
    post() { }
 
    @admin
    delete() { }
 
    @owner
    put() { }
}