A configuration object checked with satisfies can still coexist with an unchecked API response that violates the same interface. The operator helps verify code the compiler can inspect. External values need a runtime boundary that accepts, rejects, and returns a deliberately chosen type.
The example uses TypeScript 5.9 in strict mode and Node.js 24. It separates a checked static configuration, a deliberately invalid compiler fixture, and an external-input parser. No endpoint, file loader, or environment-variable reader is hidden behind the example.
Check authored configuration while retaining useful inference
TypeScript's 4.9 release notes introduce satisfies as checking expression compatibility without changing the expression's resulting type. That can make it useful for authored configuration whose individual fields should remain convenient to use after a shape check.
Save this as config-example.ts in a disposable directory with TypeScript 5.9 available locally:
type Limit = number | { max: number };
const limits = {
preview: 20,
export: { max: 100 }
} satisfies Record<"preview" | "export", Limit>;
const displayLimit: string = limits.preview.toFixed(0);
const exportMaximum: number = limits.export.max;
if (displayLimit !== "20" || exportMaximum !== 100) throw new Error("Unexpected static configuration");
The shape check permits both supported representations, while the two property accesses retain the useful types inferred for their actual expressions. A broad annotation on the entire object can instead require narrowing a property's union before using one representation's methods.
This does not freeze the object or make every nested value a literal type. Use readonly types or runtime immutability controls when the design requires them. Keep those requirements separate from checking that the initial authored object conforms to the supported shape.
Also retain a compiler check for misspelled required keys. An object that never reaches a consuming code path can otherwise look harmless during manual execution while omitting a configuration branch the application expects to exist.
Treat external data as unknown until accepted
The type-assertion documentation explains that assertions supply no runtime check. The narrowing handbook explains how checks refine values through control flow. For this synthetic boundary, the external contract is exactly one own field, max, containing an integer between 1 and 100.
Append the parser to the same file:
type ParsedLimit = { max: number };
function parseLimit(value: unknown): ParsedLimit | null {
if (value === null || typeof value !== "object" || Array.isArray(value)) return null;
if (Object.keys(value).length !== 1 || !Object.hasOwn(value, "max")) return null;
if (!("max" in value) || typeof value.max !== "number") return null;
if (!Number.isInteger(value.max) || value.max < 1 || value.max > 100) return null;
return { max: value.max };
}
const accepted = parseLimit(JSON.parse('{"max":40}'));
if (accepted?.max !== 40) throw new Error("Expected accepted input");
for (const input of [null, [], { max: "40" }, { max: 101 }, { max: 40, extra: true }]) {
if (parseLimit(input) !== null) throw new Error("Accepted invalid input");
}
The explicit return type makes the runtime promise visible. The parser constructs a fresh result instead of returning an unchecked input object. It rejects extra enumerable own fields according to this example's JSON-record contract. It is not intended to inspect proxies, arbitrary accessor objects, or deeply nested structures supplied by in-process plugins.
In an application, catch malformed JSON before calling the object parser and bound the transport input before buffering it. A correct object predicate cannot prevent an oversized request body from consuming resources earlier. For a larger schema, a maintained validation library can centralize those checks, but its configured policy still needs concrete accepted and rejected cases.
Verify compiler rejection separately from runtime rejection
Compile and execute the accepted example using the locally installed compiler:
tsc config-example.ts --strict --target ES2022 --module NodeNext --moduleResolution NodeNext --outDir compiled
node compiled/config-example.js
If the compiler is available through a project-local binary rather than PATH, invoke that binary directly. Do not use a command that silently downloads a different compiler version when the local prerequisite is missing.
Create a separate invalid-config.ts containing this deliberate static failure:
const invalid = { preview: "20" } satisfies Record<"preview", number>;
Compile that file with the same strict settings and expect a nonzero exit status because a string does not satisfy the numeric field. Keep the negative fixture separate so a successful example build does not depend on suppressing an error. It needs no error-suppression comment.
The runtime invalid records exercise a different failure. They must return null after compilation has already completed. A compiler failure cannot establish those runtime outcomes, and a parser test cannot establish that the authored configuration retains the desired inferred property types.
Keep type guarantees attached to their actual scope
Use satisfies when a developer-authored expression should be checked against a shape while remaining useful in its inferred form. Use a runtime parser when values arrive from JSON, storage, a network, or another untrusted boundary. Use both when static defaults and external overrides meet.
When defaults and overrides are combined, validate the resulting supported fields rather than recursively spreading arbitrary input into application configuration. The explicit-record parsing guide explains why write semantics matter beyond type compatibility.
Return a bounded client error for rejected data and leave persistent state unchanged. Avoid sending compiler diagnostics or stack traces to the client; those concern implementation, not a user's correction task. Keep one test that proves a bad static configuration cannot compile and another that proves a bad external value cannot cross the runtime boundary. Together they describe precisely where the application's claimed type guarantee begins.
Was this guide useful?
Your rating helps us prioritize clearer, more practical technical content.
Review diffs, run checks, and prepare the release in Arezgit.
Keep Git review, security scanning, API checks, database inspection, and release preparation together in one local desktop application.
Explore Arezgit