MediumType Inference💻 Output Question

Const assertion — literal type vs widened type

💡

Hint

TypeScript `as const` creates readonly literal types. Without it, object property types are widened to their base type (string, number) and remain mutable. Object.freeze() is the runtime equivalent of `as const`.

What does this output?

// Simulate what TypeScript infers — const narrows to literal, let widens
const direction = 'north';  // inferred: "north" (literal)
let mutableDir = 'north';   // inferred: string (widened)

// Demonstrate by checking type at runtime
console.log(typeof direction);        // string at runtime
console.log(direction === 'north');   // true — exact literal value

// const object without assertion — values widened
const config = { host: 'localhost', port: 3000 };
config.host = 'other';  // allowed — string, not literal "localhost"
console.log(config.host);

// Frozen object — runtime immutability
const frozen = Object.freeze({ host: 'localhost', port: 3000 });
try {
  frozen.host = 'other';  // silently fails in non-strict, throws in strict
} catch(e) {
  // swallow
}
console.log(frozen.host);

Correct Output

string
true
other
localhost

Why this output?

Explanation: typeof is always "string" at runtime regardless of literal types. config.host can be reassigned because TypeScript infers it as string (not "localhost" literal). Object.freeze prevents mutation at runtime.

Key Insight: TypeScript as const creates readonly literal types. Without it, object property types are widened to their base type (string, number) and remain mutable. Object.freeze() is the runtime equivalent of as const.

More Type Inference Output Questions

Easytypeof narrowing determines which branch runsMediumDiscriminated union narrowing via switchHardReturnType inference — function return shapes

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz