Menu

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

PHP Traits in TypeScript: 4 Idioms That Replace Them

DEV Community·Gabriel Anhaia·28 days ago
#30t4Xbkm
#idiom#typescript#php#javascript#class#null
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 A PHP developer opens a TypeScript codebase, finds three classes that all need a timestamps() behaviour, types trait , and gets a red squiggle. There is no such keyword. There never was. The PHP reflex says I will pull the methods into a base class and extend , then remembers TypeScript has single inheritance too. The reflex stalls. The honest answer is that TypeScript will never give you a trait keyword. The language design pulls in a different direction. What it gives you instead is four shapes that cover almost every case a PHP trait covers, with trade-offs you can actually see at the call site. Pick the one that matches the intent of the trait, not its syntax. Here is one PHP trait. Then four TypeScript rewrites of the same behaviour.…

Continue reading — create a free account

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

Read More