0Pricing
TypeScript Academy · Lesson

Config Layering and Defaults

Merge defaults, files, and environment overrides.

Config Layering and Defaults is a free TypeScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Configuration in Layers

Real apps assemble config from several sources: built-in defaults, a config file, and environment overrides. Later layers override earlier ones in a defined precedence order.

Precedence Order

A common order, lowest to highest priority: defaults < file config < environment variables. Environment overrides win because they suit per-deployment tweaks.

Define the Layers

Each layer is a partial config. Only the base defaults need to be complete; the others fill in or override selected fields.

interface Config { port: number; host: string; logLevel: string; }
const defaults: Config = { port: 3000, host: "localhost", logLevel: "info" };

Partial Overrides

The file and env layers are Partial<Config>, since they may specify only some keys. Typing them as partial reflects that any field can be absent.

const fileConfig: Partial<Config> = { port: 8080 };
const envConfig: Partial<Config> = { logLevel: "debug" };

Shallow Merge

For flat config, a spread merge applies precedence cleanly. Later objects override earlier keys, producing the final config.

const config: Config = { ...defaults, ...fileConfig, ...envConfig };
// { port: 8080, host: "localhost", logLevel: "debug" }

Why Order Matters

Because spread later-wins, the source listed last has the highest priority. Reorder the spreads and you change which layer overrides which.

Nested Config Needs Deep Merge

Shallow spread replaces whole nested objects. If config has nested sections, you need a deep merge that combines them recursively instead of overwriting.

interface Config { db: { host: string; port: number }; }
// { ...a, ...b } would replace the entire db object, losing keys.

A Typed Deep Merge

A recursive merge walks both objects, descending into nested records and overriding leaves. Typing the result requires a recursive type helper.

type DeepPartial<T> = { [K in keyof T]?: DeepPartial<T[K]> };
function deepMerge<T>(base: T, over: DeepPartial<T>): T {
  const out: any = { ...base };
  for (const k in over) {
    const v = (over as any)[k];
    out[k] = v && typeof v === "object" ? deepMerge((base as any)[k], v) : v;
  }
  return out;
}

DeepPartial for Override Layers

Override layers are typed as DeepPartial<Config> so they may specify any subset of nested keys while staying type-checked against the full shape.

const override: DeepPartial<Config> = { db: { port: 5433 } };
// merges into db without dropping db.host

Combine with Validation

Merge first, then validate the result with a schema. This way the final, fully-resolved config is the thing checked for correctness.

// const merged = deepMerge(defaults, override);
// const config = ConfigSchema.parse(merged);

Layering Best Practices

Keep defaults complete and safe, let files express environment-specific settings, and reserve env vars for secrets and per-deploy overrides at the top of the precedence chain.

Quick Check

Quick check on this lesson.

Recap

Layered config merges defaults, file config, and env overrides in precedence order (defaults < file < env). Flat config uses spread; nested config needs a deep merge typed with DeepPartial<Config>. Validate the merged result.

Frequently asked questions

Is the “Config Layering and Defaults” lesson free?

Yes — the full text of “Config Layering and Defaults” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Config Layering and Defaults”?

Merge defaults, files, and environment overrides. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Config Layering and Defaults” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Typing Environment Variables
  2. Schema-Validated Config
  3. Config Layering and Defaults
  4. Secrets and Type Safety
← Back to TypeScript Academy