Skip to content

StringLiteralUnion

reference
1 min readUpdated

Kind: Type

Source: src/utils/types.ts

Part of: Utils

String literal types with auto-completion

StringLiteralUnion combines a set of string literal values with string while preserving editor autocomplete for the known literals. It is used when an API accepts predefined string options but must also allow custom string values.

Definition

ts
T | (string & Record<never, never>)

Diagram

mermaid
graph LR
  A[Known string literals] --> C[StringLiteralUnion]
  B[Custom string values] --> C
  C --> D[Autocomplete for known values]
  C --> E[Accepts any string]

Usage

ts
import type { StringLiteralUnion } from "../utils/types";

type Theme = StringLiteralUnion<"light" | "dark">;

const defaultTheme: Theme = "light";
const customTheme: Theme = "system";

AI Coding Instructions

  • Use StringLiteralUnion for string options that have known values and allow arbitrary custom values.
  • Keep the known options as string literal unions, such as "light" | "dark".
  • Do not replace this type with string when autocomplete for known values is expected.
  • Use the type in public option and configuration definitions where custom string extensions are valid.

Was this page helpful?

Download as PDF
StringLiteralUnion — Hono (narrator proof)