Decorators

What is a decorator?
A decorator is just a function that runs once when the code is loaded.
It receives whatever it decorates (class, method, property…) and can replace or augment it. Decorators let us inject cross‑cutting behaviour without touching the core logic.
Class vs method decorator

Class decorator

At load time we create Proxy
, stash a reference to the original class, and hand Proxy back to the runtime. When someone later does new MobiscrollDatePicker()
, the proxy decides whether to instantiate the desktop or mobile implementation.
Method decorator

Receives the original method and metadata about what you're decorating (context.name
). Returns a new function that wraps the call in test.step
, giving us pretty test reports.
Method decorator

Take‑aways
-
Decorators execute at load time, not at call time.
-
A class decorator can replace the whole class, a method decorator can only replace that one method.
-
In our codebase,
@Desktop
+@Mobile
dynamically pick the right implementation, while@step
adds nice Playwright reporting
Thank You!
Questions?
Typescript decorators
By TenantCloud
Typescript decorators
- 134