QA
Intermediate LevelJavaScript & TypeScript

How does satisfies operator work in TypeScript 4.9+?

❓ Interview Prompt: Compare satisfies operator vs explicit type annotations for validating object shapes while preserving exact inferred types.

Introduced in TypeScript 4.9, the `satisfies` operator validates that an expression matches a target type without mutating or widening the inferred type of the expression.

The Problem With Type Annotations (const obj: Type):

Annotating an object widens properties to the general interface, losing specific literal types or missing optional key autocomplete.

The satisfies Solution (const obj = { ... } satisfies Type):

Validates structure against Type, but retains the exact specific inferred literal types and property types of the object!

Code Implementation Example

typescript
type Colors = 'red' | 'green' | 'blue';
type Palette = Record<Colors, string | [number, number, number]>;

// With satisfies: Validates shape AND preserves exact property types!
const palette = {
  red: '#ff0000',
  green: [0, 255, 0],
  blue: '#0000ff'
} satisfies Palette;

// ✅ TS knows red is string (can call toUpperCase)
palette.red.toUpperCase();

// ✅ TS knows green is array tuple (can call map)
palette.green.map(n => n * 2);

// palette.purple; // ❌ Compile Error: Property 'purple' does not exist!
💡Using satisfies operator to validate contract while retaining narrow property type inference.
🎯 Interview Key Takeaway

Use satisfies operator to validate that an object matches a contract while preserving exact property type inference.

#TypeScript#satisfies#TS 4.9#Intermediate
Back to all JavaScript & TypeScript Questions→

More JavaScript & TypeScript Interview Questions

Beginner LevelJavaScript & TypeScript

What are the differences between var, let, and const in JavaScript?

Beginner LevelJavaScript & TypeScript

What are the JavaScript data types and the difference between primitives and objects?

Beginner LevelJavaScript & TypeScript

What is the difference between == (loose equality) and === (strict equality)?