Menu

Decorating a Promise with convenience methods at runtime without subclassing or Proxy
📰

Decorating a Promise with convenience methods at runtime without subclassing or Proxy

Reddit r/typescript·u/OtherwisePush6424·7 months ago
#lyectZrE
Reading 0:00
15s threshold

How to attach optional methods like .json() and .text() directly to a Promise instance using property descriptors, a Symbol-based idempotency guard, and an intersection type, without changing what await returns, without subclassing, and without a Proxy layer.

The TypeScript angle: the intersection type Promise & ResponseShortcuts is a shape guarantee, not a behavioral one. The post is about what that means and where it falls short.

blog.gaborkoos.com