Menu

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

From Java's Optional<T> to TypeScript: Three Idioms That Replace It Cleanly

DEV Community·Gabriel Anhaia·28 days ago
#6MnYpyFW
Reading 0:00
15s threshold

Book: Kotlin and Java to TypeScript — A Bridge for JVM 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 join a TypeScript codebase as a Java engineer, see a function returning User | undefined , and reach for the obvious move — you wrap it. type Optional &lt; T &gt; = { value : T } | { value : null }; Enter fullscreen mode Exit fullscreen mode Then you add .map() , .flatMap() , .orElse() methods. Two weeks later the code review backs up. The team is half-fluent in this homemade Optional type, half-fluent in plain TypeScript, and the boundary between them is where bugs hide. Half the codebase wraps, half doesn't, and the boundary leaks. Functions that take a User | undefined get called with an Optional&lt;User&gt; value that contains a non-null user. Both compile. Neither is what the caller meant.…

Continue reading — create a free account

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

Read More