Menu

Post image 1
Post image 2
Post image 3
1 / 3
0

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

DEV Community·Gabriel Anhaia·26 days ago
#DhlWFxPT
#pattern#typescript#php#orderstatus#paid#enum
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. At the call site, OrderStatus.label(status) looks almost exactly like Status::Active->label() did in PHP.…

Continue reading — create a free account

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

Read More