Menu

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

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

DEV Community·Gabriel Anhaia·30 days ago
#enXt6Y7K
#why#typescript#java#optional#undefined#user
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 &lt; T &gt; { private constructor ( private readonly value : T | undefined ) {} static of &lt; T &gt; ( value : T ): Optional &lt; T &gt; { return new Optional ( value ); } static empty &lt; T &gt; (): Optional &lt; T &gt; { return new Optional &lt; T &gt; ( undefined ); } static ofNullable &lt; T &gt; ( value : T | undefined ): Optional &lt; T &gt; { 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.

Read More