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

Original title: Adding Go's Defer to the TypeScript Compiler

> Hacker News story 49147853 · language: en · submitted 2026-08-02
> Web version: https://zeli.app/story/49147853
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 20 · by [healeycodes](<https://news.ycombinator.com/user?id=healeycodes>) · 💬 28
[original](<https://healeycodes.com/adding-defer-to-the-typescript-compiler>) · [HN discussion](<https://news.ycombinator.com/item?id=49147853>)

## Summary

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.

## Key quote

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

## HN discussion (28 comments)

### [throwaway81523](<https://news.ycombinator.com/item?id=49148618>)

> 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.

### [geostyx](<https://news.ycombinator.com/item?id=49150176>)

> 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

### [fg137](<https://news.ycombinator.com/item?id=49149694>)

> Would call stack/source map reasonably work with this?

---

## Cite

- Page: https://zeli.app/story/49147853
- Date: 2026-08-02
- Source: healeycodes.com (https://healeycodes.com/adding-defer-to-the-typescript-compiler)
- Hacker News: https://news.ycombinator.com/item?id=49147853

## Other languages

- zh: https://zeli.app/zh/story/49147853 · markdown: https://zeli.app/zh/story/49147853.md
- ja: https://zeli.app/ja/story/49147853 · markdown: https://zeli.app/ja/story/49147853.md
- de: https://zeli.app/de/story/49147853 · markdown: https://zeli.app/de/story/49147853.md
