Primitives, literal types and as const
Relay logs at one of four levels. LOG_LEVEL arrives from the environment as a string, and a
string can be anything: "warn", "WARN", "warning", "". If the code after the config loader
only knows “it’s a string”, every comparison downstream is a guess. This lesson makes the compiler
know the exact set of values.
The primitive types
Section titled “The primitive types”| Type | Values | In Relay |
|---|---|---|
string |
text | URLs, names, raw env vars |
number |
64-bit floats, including NaN and Infinity |
ports, timeouts |
boolean |
true, false |
feature switches |
bigint |
integers of any size (10n) |
byte counts past 2⁵³ |
symbol |
unique keys | rarely in app code |
null, undefined |
absence | a missing env var is undefined |
number has no separate integer type. Number("8080.5") is a valid number, and so is
Number("abc"), which is NaN. The type can’t tell a good port from a bad one. That check is
yours, as in the last lesson’s exercise.
Literal types
Section titled “Literal types”A literal type is a type with exactly one value. "info" is a type, and so is 8080. A union of
literals lists every value allowed:
type LogLevel = "debug" | "info" | "warn" | "error"
const level: LogLevel = "warn"// @ts-expect-error: "warning" is not one of the fourconst typo: LogLevel = "warning"Widening: what const and let infer
Section titled “Widening: what const and let infer”TypeScript infers the narrowest type that stays true for the variable’s lifetime. A const can
never change, so it keeps the literal. A let can be reassigned, so it widens to the primitive.
const a = "info" // type "info"let b = "info" // type string
type LogLevel = "debug" | "info" | "warn" | "error"function setLevel(level: LogLevel) { return level}
setLevel(a)// @ts-expect-error: string is not assignable to LogLevelsetLevel(b)Objects and arrays widen even when declared with const, because their contents can still change:
const LEVELS = ["debug", "info", "warn", "error"] // string[]const DEFAULTS = { level: "info", port: 8080 } // { level: string; port: number }
DEFAULTS.level = "anything" // allowed: the property is a plain stringLEVELS.push("verbose") // allowed tooas const
Section titled “as const”as const says “this value will never change”, so nothing widens. Arrays become readonly tuples
and properties become readonly literals:
const LEVELS = ["debug", "info", "warn", "error"] as const// readonly ["debug", "info", "warn", "error"]
type LogLevel = (typeof LEVELS)[number]// "debug" | "info" | "warn" | "error"
// @ts-expect-error: a readonly tuple has no pushLEVELS.push("verbose")(typeof LEVELS)[number] reads as “the type of any element of LEVELS”. The list is now written
once and is both the runtime array (for validation and error messages) and the type. Adding a
fifth level updates both.
satisfies: check without widening
Section titled “satisfies: check without widening”A type annotation replaces what TypeScript inferred. satisfies checks the value against a type and
keeps what was inferred. Here it guarantees RANK has a number for every level, while the
as const literals stay exact:
const LEVELS = ["debug", "info", "warn", "error"] as consttype LogLevel = (typeof LEVELS)[number]
const RANK = { debug: 10, info: 20, warn: 30, error: 40 } as const satisfies Record<LogLevel, number>// RANK.warn has type 30
// @ts-expect-error: property 'error' is missingconst PARTIAL = { debug: 10, info: 20, warn: 30 } as const satisfies Record<LogLevel, number>Forget a level and the error points at the object. Without satisfies, the missing key would
surface later as an undefined rank.
Checking a string against the list
Section titled “Checking a string against the list”The obvious check doesn’t compile:
const LEVELS = ["debug", "info", "warn", "error"] as constdeclare const raw: string
// @ts-expect-error: string is not assignable to "debug" | "info" | "warn" | "error"LEVELS.includes(raw)includes on a tuple of literals only accepts those literals, and raw is any string. Use find.
It compares each element with raw and returns the element itself, which already has the literal
type, or undefined:
const LEVELS = ["debug", "info", "warn", "error"] as constdeclare const raw: string
const level = LEVELS.find((l) => l === raw)// "debug" | "info" | "warn" | "error" | undefinedNo cast is needed. After a check for undefined, level is a LogLevel.
Exercise
Section titled “Exercise”Make LogLevel the four-literal union (the hidden tests check the type), then make parseLevel
reject anything outside it. Hover LogLevel before and after your fix.
- A literal type has one value. A union of literals lists every allowed value.
constkeeps literals for primitives. Objects and arrays widen unless you addas const.(typeof XS)[number]turns anas constarray into a union: one source for the values and the type.satisfieschecks a value against a type without losing its inferred literals.- Validate with
find, notas.
Further reading
Section titled “Further reading”- Handbook: Everyday Types: literal types, widening and
as const.