Dev Helpers
← Back

Enforce a mutually exclusive config source

Enforce a mutually exclusive config source using TypeScript.

Open this example in the interactive library

Source code

type Config = {
  mode: "dev" | "prod";
  retries: number;
  tags: readonly string[];
};
const defaults: Config = { mode: "dev", retries: 2, tags: ["api"] };
console.log(
  (() => {
    type Source =
      { file: string; inline?: never } | { inline: Config; file?: never };
    const s: Source = { file: "local.json" };
    return s.file;
  })(),
);

Sample output

local.json