LANGUAGES Signal 366
Adding Go's Defer to the TypeScript Compiler
The exercise demonstrates that TypeScript's existing AST-to-AST transformation pipeline can be extended for novel syntax, but also shows why some language features don't translate well across ecosystems. The try/finally pattern already covers the use case, and defer introduces runtime overhead and semantic complexity that may not justify the syntactic convenience.
Written by elseif from the cluster below · every claim links back to a sourceThe three things worth knowing
The implementation transforms defer statements into a stack of closures executed in a finally block, matching Go's last-in-first-out semantics and immediate evaluation of callee, receiver, and argument values.
The author leveraged tsc's existing rewrite infrastructure, adding a DeferStatement syntax kind to the parser and checker validation that restricts defer to function bodies and disallows it in generators.
Despite successfully implementing the feature with proper Go-like semantics, the author concluded that defer probably shouldn't exist in TypeScript, suggesting the experiment revealed more friction than practical value.
THE CLUSTER