Course contentsShow
← TypeScript
Lesson 178 of 1,7964. Type InferenceFree lesson

Object Literals Widening to General Types

How object property types widen from literals to primitives and when this causes assignment problems.

Object Literals Widening to General Types

You'll learn: How TypeScript widens object property types from specific literals to broader primitives, and when this causes unexpected assignment errors.

The Core Idea

When you declare an object with let, TypeScript doesn't lock the property types to their exact literal values. Instead, it widens them to general primitive types—just like it did with arrays.

let user = {
  name: "Alice",
  age: 30
};

TypeScript infers user as { name: string, age: number }, not { name: "Alice", age: 30 }. This makes sense for mutability—you'll probably change these values later. But this widening can clash with stricter expectations:

const config = {
  mode: "development"
};

// Elsewhere, expecting the literal "development"
function startServer(mode: "development" | "production") {
  // ...
}

startServer(config.mode); // Error: string is not assignable to "development" | "production"

The property config.mode was inferred as string, not "development", so it fails the stricter check.

Why This Happens

TypeScript assumes object properties declared with let or in non-const contexts will change. Widening to string instead of "development" allows reassignment flexibility—but sacrifices literal precision.

The Fix

Use const for the object itself (which doesn't prevent widening) combined with as const to lock properties to literal types—or explicitly annotate the type you need. (You'll learn as const in future lessons.)

Key Takeaway: Object property types widen from literals to primitives by default with let, which can break assignments that expect exact literal types.