> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript Typeof

The `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 of `typeof` 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.

```typescript theme={"dark"}
const user = { name: "Alice", age: 30 };

// Value context: JS typeof returns a string indicating the runtime data type. 
// TS uses this as a Type Guard for control flow analysis.
function process(val: string | typeof user) {
  if (typeof val === "string") {
    val.toUpperCase(); // TS narrows 'val' to string
  }
}

// Type context: TS typeof returns the type { name: string; age: number; }
type UserType = typeof user; 
```

## Syntax and Operands

In a type context, the TypeScript `typeof` operator accepts identifiers, property access chains, and inline module imports. It cannot evaluate arbitrary expressions, mathematical operations, or function invocations.

**Valid Operands:**

```typescript theme={"dark"}
const config = { endpoint: "/api", retries: 3 };

// Identifier
type ConfigType = typeof config; 

// Property access chain
type EndpointType = typeof config.endpoint; // Type is 'string'

// Import type: Extracts the type of a module without a runtime import
type FsModule = typeof import("fs");
```

**Invalid Operands:**

```typescript theme={"dark"}
function calculate() { return 42; }

// Error: Cannot use typeof on a function call expression
type Result = typeof calculate(); 

// Error: Cannot use typeof on an arithmetic expression
type MathResult = typeof (10 + 20); 
```

## 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.

```typescript theme={"dark"}
class Database {
  static connectionLimit = 10;
  connect() {}
}

// Refers to the instance type: { connect: () => void }
let dbInstance: Database;

// Refers to the constructor type: new () => Database, plus static members
type DatabaseConstructor = typeof Database;

// Accessing static members on the constructor type
type LimitType = DatabaseConstructor["connectionLimit"]; // Type is 'number'
```

## 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.

```typescript theme={"dark"}
function format(input: string, toUpper: boolean): string {
  return toUpper ? input.toUpperCase() : input;
}

// Type is: (input: string, toUpper: boolean) => string
type FormatFunction = typeof format;
```

To extract only the return type of a function, `typeof` must be composed with the `ReturnType` utility type:

```typescript theme={"dark"}
// Type is: string
type FormatReturn = ReturnType<typeof format>;
```

## 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.

```typescript theme={"dark"}
const mutableColors = ["red", "blue"];
// Type is: string[]
type MutableType = typeof mutableColors;

const immutableColors = ["red", "blue"] as const;
// Type is: readonly ["red", "blue"]
type ImmutableType = typeof immutableColors;
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor TypeScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
