Q22: What is the `typeof` operator in Type Contexts?

Difficulty:EasyTopic:Type Operators

In a type expression context, typeof refers to the TypeScript type of a variable or property value, allowing you to infer types directly from runtime variables without writing manual interfaces.

typeof Type Context Extraction
const defaultTheme = {
  color: "blue",
  fontSize: 14,
  isDark: false
};

// Extract type structure from runtime variable
type Theme = typeof defaultTheme;
// Resulting type: { color: string; fontSize: number; isDark: boolean; }

typeof converts runtime JavaScript object structures into static TypeScript types.

Key Takeaways
  • Use typeof in type contexts to derive static type contracts directly from runtime JavaScript values.