Excess Property Checks Guard Config Literals
Type fresh config object literals against an interface so a misspelled key fails compilation, while values from wider variables skip the extra-key check.
Code
interface DeployConfig {
region: string;
replicas: number;
tls?: boolean;
}
// Fresh object literals get excess property checking:
// every declared key must exist and no unknown key may appear.
const config: DeployConfig = {
region: 'eu-central-1',
replicas: 3,
tls: true,
};
console.log('valid config:', config);
// A misspelled key on a fresh literal fails compilation:
// "Object literal may only specify known properties, and 'replcas' does not
// exist in type 'DeployConfig'." The typo never reaches runtime.
// Excess checks target fresh literals, not wider variables:
const raw = { region: 'us-east-1', replicas: 2, debug: true };
const widened: DeployConfig = raw; // structural match, no extra-key error
console.log('widened from variable:', widened);Output
Captured from a sandboxed run of this snippet in the build pipeline.
valid config: { region: 'eu-central-1', replicas: 3, tls: true }
widened from variable: { region: 'us-east-1', replicas: 2, debug: true }How it works
The file’s state is a handful of top-level bindings: raw.
The top-level object literal pins region, replicas, tls as plain values — no derivation, no imports behind them.
Applying it
The demo output is labeled (valid config:, widened from variable:), so each printed line points back to the operation it originated from.
Statements that matter
const widened: DeployConfig = raw; // structural match, no extra-key error
console.log('widened from variable:', widened);
const raw = { region: 'us-east-1', replicas: 2, debug: true };
replicas: number;
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
- Any setup needed beyond the language runtime?
- None — the language runtime is the only requirement. Its state lives in module-level bindings: `raw`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.