Menu

1 / 2

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

DEV Community·Gabriel Anhaia·6 months ago
#6MnYpyFW
#idiom#typescript#java#programming#undefined
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 < T > = { 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<User> value that contains a non-null user. Both

compile.…

Continue reading — create a free account

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