Skip to content

IsLiteralUnion

reference
1 min readUpdated

Kind: Type

Source: src/validator/utils.ts

Checks if T is a literal union type (e.g., 'asc' | 'desc') that should be preserved in input types. Returns true for union literals, false for single literals or wide types.

IsLiteralUnion<T> evaluates whether T is a union of literal values, such as 'asc' | 'desc'. Validator input-type logic uses this result to preserve literal unions while treating single literals and widened types differently.

Definition

ts
[Exclude<T, undefined>] extends [Base] ? [Exclude<T, undefined>] extends [UnionToIntersection<Exclude<T, undefined>>] ? false : true : false

Diagram

mermaid
graph LR
  T[Input type T] --> Check{Is T a literal union?}
  Check -->|Yes| True[true: preserve input type]
  Check -->|No| False[false: handle as non-union or wide type]

Usage

ts
import type { IsLiteralUnion } from './validator/utils'

type SortDirection = 'asc' | 'desc'
type SingleDirection = 'asc'
type WideString = string

type IsSortDirection = IsLiteralUnion<SortDirection> // true
type IsSingleDirection = IsLiteralUnion<SingleDirection> // false
type IsWideString = IsLiteralUnion<WideString> // false

AI Coding Instructions

  • Use IsLiteralUnion<T> only in type-level conditional logic; it has no runtime value.
  • Preserve unions of literal strings or numbers when this type evaluates to true.
  • Do not treat a single literal, such as 'asc', as a literal union.
  • Check widened types such as string, number, and boolean separately when changing validator input-type behavior.

Was this page helpful?

Download as PDF
IsLiteralUnion — Hono (narrator proof)