Menu

1 / 2

PHP Enum Methods to TypeScript: Why Discriminated Unions + namespace Beats class enums

DEV Community·Gabriel Anhaia·6 months ago
#DhlWFxPT
#pattern#typescript#php#orderstatus#paid
Reading 0:00
15s threshold

Book: PHP to TypeScript — A Bridge for Modern PHP 8+ Developers

Also by me: The TypeScript Library — the 5-book collection

My project: Hermes IDE | GitHub — an IDE for developers who ship with Claude Code and other AI coding tools

Me: xgabriel.com | GitHub

You write enum Status in TypeScript and assume the bridge from PHP is built. It is not. PHP's enum is a runtime class with method dispatch and exhaustive match . TypeScript's enum is a compile-time hint that emits an unwanted IIFE. Same keyword, different semantics. The TypeScript handbook documents the pitfalls of enum and points to literal-type unions as the alternative for almost every case.

There is a clean translation. It is not the keyword. It is a discriminated union plus a same-named namespace that holds the companion methods. The values live in the type. The methods sit in a same-named namespace next to it.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.