Skip to content

TypeScript · correctness

prefer-discriminated-union

eslint:prefer-discriminated-union

Flag flat result objects with a required positive boolean status and optional success/failure payloads.

Default
error
Fix
none
Languages
typescript

Why

A boolean status plus optional branch data permits contradictory and incomplete states.

Fix

Represent each result branch as a discriminated union member with its required payload.

Before / after

Executed by this rule’s unit tests.

Before

Do not make both result payloads optional

src/result.ts · focus
type Result = { ok: boolean; data?: string; error?: string };

After

Use explicit result branches

src/result.ts · focus
type Result = { ok: true; data: string } | { ok: false; error: string };

Limits

  • Only local object shapes with recognized positive status and payload names are inspected.

Message IDs

preferDiscriminatedUnion