TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
typeof operator in TypeScript is a compile-time type query operator that extracts the inferred or explicitly declared type of an existing runtime value (such as a variable, object property, function, or class) and projects it into the type space. It allows developers to capture the structural shape or signature of existing bindings without duplicating declarations.
Type Space vs. Value Space
TypeScript distinguishes between the value space (runtime JavaScript) and the type space (compile-time TypeScript). The behavior oftypeof depends entirely on the context in which it is invoked:
- Value Context (JavaScript): Evaluates at runtime and returns a string indicating the operand’s runtime data type, which includes both primitives (e.g.,
"string","number") and non-primitives (e.g.,"object","function"). In TypeScript, these runtime checks function as Type Guards. The compiler utilizes control flow analysis to narrow the type of a variable within a conditional block based on this runtime evaluation. - Type Context (TypeScript): Evaluates at compile-time and returns the exact TypeScript type signature of the identifier.
Syntax and Operands
In a type context, the TypeScripttypeof operator accepts identifiers, property access chains, and inline module imports. It cannot evaluate arbitrary expressions, mathematical operations, or function invocations.
Valid Operands:
Behavior with Classes
In TypeScript, class declarations create both a type (representing the instance) and a value (the constructor function). Using a class identifier directly as a type refers to the instance type. To extract the constructor type—which includes the constructor signature and any static members—typeof must be applied to the class identifier.
Behavior with Functions
When applied to a function identifier,typeof extracts the complete function signature, including parameter types and the return type. It does not execute the function.
typeof must be composed with the ReturnType utility type:
Interaction with Literal Widening
By default, TypeScript widens the types of mutable variables.typeof captures this widened type. If the target value is declared with a const assertion (as const), typeof captures the exact, narrowed literal type, including readonly modifiers.
Master TypeScript with Deep Grasping Methodology!Learn More





