Literal Types
Literal types constrain primitive types to specific values.
info
This feature was added in v0.61.0.
Syntax
"specific_string"
42
true
false
Supported Literal Types
| Base Type | Literal Example |
|---|---|
string | "bug", "feature", "red" |
int | 1, 42, -5 |
bool | true, false |
Examples
String Literals
function ClassifyIssue(description: string) -> "bug" | "enhancement" {
client "openai/gpt-4o"
prompt #"
Classify the issue based on the description:
{{ ctx.output_format }}
{{ _.role("user") }}
{{ description }}
"#
}
Mixed Literals
function GetStatus() -> "active" | "inactive" | 0 | 1 {
client "openai/gpt-4o"
prompt #"
Return the status.
{{ ctx.output_format }}
"#
}
Literal Unions
"str" | 1 | false
Usage in Maps
Literal strings can be used as map keys:
map<"A" | "B" | "C", string>
Language Equivalents
| BAML | Python | TypeScript | Go |
|---|---|---|---|
"str" | 1 | false | Union[Literal["str"], Literal[1], Literal[False]] | "str" | 1 | false | Union3StringOrIntOrBool |
"red" | "green" | "blue" | Literal["red", "green", "blue"] | "red" | "green" | "blue" | string (with validation) |