From five optional fields to a discriminated union: CLI parsing with Optique 1.0

The problem: types and validators are strangers
If you’ve wired up a CLI with Commander.js or Yargs, you know the drill: declare options, add .conflicts() or .implies(), and the runtime validator will shout when the user mixes flags they shouldn’t. Nice. But what about the type system? TypeScript still hands you a plain object of optional fields — five independent maybe-strings — and nothing in that shape tells you which authentication mode you actually picked. Who hasn’t written a guard and then slapped on a non-null assertion, praying the user didn’t lie? That mismatch — validator knows, types don’t — is the emotional crux here: it’s annoying, error-prone, and surprisingly common.
Optique 1.0: discriminated unions for arguments
It has been reported that Hong Minhee tagged Optique 1.0 after growing a side project into a proper tool. The key idea is a small but powerful one: make the parser produce types that know their branch. Instead of five unrelated optional fields, you get a discriminated-union shape that reflects “token auth” vs “basic auth” vs “OAuth” at the type level. That lets the compiler help you. No more runtime-only promises; the type system and validator sing from the same hymn sheet.
Beyond argv: one parser, many sources
Optique reportedly doesn’t stop at argv. The same parsing structure can pull from environment variables, config files, or interactive prompts, so your auth logic is consistent regardless of where values came from. That’s neat because modern CLIs rarely live in a vacuum — they read env, they melt into config, they ask the user — and keeping those sources aligned with type-level guarantees cuts down on surprising runtime checks.
Trade-offs and where it isn’t the right choice
This isn’t a slam on Commander.js or Yargs — they’re solid, battle-tested tools with plenty of users. Optique aims at a different point on the spectrum: stronger compile-time guarantees and multi-source parsing. It has been reported that some workflows or teams might find the extra type machinery overkill; sometimes simplicity and familiarity win. But if you’ve long wanted your type checker to call out the same logical branches your validator enforces, Optique 1.0 looks like a tidy step forward.
Sources: hackers.pub, Lobsters
Comments