The satisfies Operator for Config Objects
Use satisfies to check a config literal against a broad type while preserving its literal inference, catching typos without widening values.
Code
type Theme = "light" | "dark" | "system";
interface AppConfig {
theme: Theme;
locale: string;
features: Record<string, boolean>;
}
export const config = {
theme: "dark",
locale: "en-US",
features: { search: true, beta: false },
} satisfies AppConfig;
// config.theme is inferred as "dark", not widened to Theme:
export type ThemeValue = typeof config.theme;
export function resolveTheme(value: ThemeValue, prefersDark: boolean): "light" | "dark" {
if (value === "system") return prefersDark ? "dark" : "light";
return value;
}How the snippet works
Count the function resolveTheme as the entire surface area of The satisfies Operator for Config Objects.
Watch out for
- The guard
value === "system"short-circuits the flow with an earlyreturnwhen its condition holds.
Applying it
The main signature reads export const config =
Import config from the file and call it — the rest of the code is implementation detail.
Statements that matter
if (value === "system") return prefersDark ? "dark" : "light";
Interactive Tools for This Topic
Dotenv File ParserParse .env file content into a JSON object, handling comments, export prefixes, single quotes, and double-quoted escape sequences.INI File ParserParse INI configuration files into nested JSON with section objects, global keys, and both semicolon and hash comment support.JSON to TOML ConverterTurn a JSON object into TOML, emitting nested objects as tables and arrays of objects as array-of-tables blocks.JSON to YAML ConverterConvert JSON to readable YAML with nested block mappings, sequences, and quoting only where it is required.
FAQ
- Does the code pull in any external packages?
- No external packages are involved; every call resolves to a built-in API. The file defines `resolveTheme` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.