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.

TypescriptConfig Management

Code

typescriptsnippet
23 lines
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

FAQ

Any setup needed beyond the language runtime?
None — the language runtime is the only requirement. Its state lives in module-level bindings: `raw`.