Skip to content

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.

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.

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 four
const typo: LogLevel = "warning"

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 LogLevel
setLevel(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 string
LEVELS.push("verbose") // allowed too

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 push
LEVELS.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.

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 const
type 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 missing
const 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.

The obvious check doesn’t compile:

const LEVELS = ["debug", "info", "warn", "error"] as const
declare 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 const
declare const raw: string
const level = LEVELS.find((l) => l === raw)
// "debug" | "info" | "warn" | "error" | undefined

No cast is needed. After a check for undefined, level is a LogLevel.

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.
  • const keeps literals for primitives. Objects and arrays widen unless you add as const.
  • (typeof XS)[number] turns an as const array into a union: one source for the values and the type.
  • satisfies checks a value against a type without losing its inferred literals.
  • Validate with find, not as.