I Added Go's defer to TypeScript — and It Shouldn't Exist

Adding Go's Defer to the TypeScript Compiler

A developer explores adding Go's defer statement to the TypeScript compiler, transforming it into a try/finally with a stack of closures. The implementation reveals fundamental mismatches: JavaScript's exception-based error handling forces complex aggregation policies for cleanup failures, unlike Go's value-based errors. The author concludes defer doesn't belong in TypeScript and suggests the ECMAScript Explicit Resource Management proposal as a better fit.

The more edge cases I implemented, the less convinced I became that `defer` belongs in TypeScript.
  1. throwaway81523

    Article describes try/finally as a hack to get the effect of defer, but it looks to me like it's the other way around? Try/finally is more traditional, in one form or another.

  2. geostyx

    I built a library to do something similar: https://www.npmjs.com/package/yaplib

    defer is the thing I miss the most coming from Go

  3. fg137

    Would call stack/source map reasonably work with this?

More from this day

2026-08-02