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
tsT | (string & Record<never, never>)
Diagram
mermaidgraph LR A[Known string literals] --> C[StringLiteralUnion] B[Custom string values] --> C C --> D[Autocomplete for known values] C --> E[Accepts any string]
Usage
tsimport type { StringLiteralUnion } from "../utils/types";
type Theme = StringLiteralUnion<"light" | "dark">;
const defaultTheme: Theme = "light";
const customTheme: Theme = "system";
AI Coding Instructions
- Use
StringLiteralUnionfor 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
stringwhen 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?