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.

TypescriptConfig Management

Code

typescriptsnippet
21 lines
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 early return when 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

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.