Menu

1 / 2

Java's `Optional<T>` Is Dead. TypeScript Gave You Better.

DEV Community·Gabriel Anhaia·6 months ago
#enXt6Y7K
#why#typescript#java#optional#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

A Java developer joins a TypeScript service. The repository's findUserById returns User | undefined . Their hand reaches for the muscle memory: wrap it. Make it safe. Give the caller a thing that signals absence the way a Java codebase has signalled absence since 2014.

class Optional < T > { private constructor ( private readonly value : T | undefined ) {}

static of < T > ( value : T ): Optional < T > { return new Optional ( value ); } static empty < T > (): Optional < T > { return new Optional < T > ( undefined ); } static ofNullable < T > ( value : T | undefined ): Optional < T > { return new Optional ( value ); }

isPresent (): boolean { return this . value !== undefined ; } get (): T { if ( this .…

Continue reading — create a free account

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