Question presented to candidate: "If you have a class representing a database connection, how would you combine a decorator with using/await using so that every instance is automatically, correctly disposable — without manually writing [Symbol.dispose] on every single class yourself?"
What a strong answer should cover:
- 📌 Interview term: the real, combined pattern — a class decorator can programmatically ADD a
[Symbol.dispose]/[Symbol.asyncDispose]method to every class it's applied to, so any instance of that class automatically, correctly works withusing/await using(covered in more depth in this bank's own dedicated Explicit Resource Management question) — without hand-writing the disposal method on each individual class. - 📌 Interview term: the real, direct honesty check — verified directly, in both Node v24 and a real, current browser: the DECORATOR half of this pattern genuinely cannot run natively anywhere today (a real
SyntaxError, matching this bank's own dedicated Decorators question) — only theusinghalf is genuinely, directly runnable. - 📌 Interview term:
usingon its own, fully verified — verified directly: a resource implementing[Symbol.dispose](written by hand, without a decorator) is genuinely, automatically disposed when ausing-declared block exits, in strict reverse declaration order for multiple resources — the real mechanism the decorator half would ultimately be automating. - A precise answer names the real, accurate combined syntax (a class decorator using
context.addInitializerto attach the dispose method to each new instance) while being explicit that it is illustrative Stage 3 syntax, not something that can be pasted and run today. - A precise answer names the real, practical motivation: without this combination, every class needing disposal (a DB connection, a file handle, a lock) must hand-write its own
[Symbol.dispose]; a shared@disposable-style decorator would let that boilerplate be written ONCE and reused across many classes.
Clarifying questions expected:
- None — this is a definitional/technical question; honestly separating what IS directly verifiable (using alone) from what ISN'T (the decorator half) is the strong signal.
Code / implementation expected: Yes for the using-only portion (genuinely runnable, verified directly); the combined decorator+using syntax is shown as real, accurate reference code, honestly marked non-runnable.