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

The `keyof` operator is a type-level operator in TypeScript that takes an object type and produces a union type of its keys. The resulting type can consist of string, number, and symbol literals, or the broad `string`, `number`, and `symbol` primitive types depending on the target type's shape. It operates exclusively within the type system during static analysis and has no runtime equivalent.

```typescript theme={"dark"}
type ExtractedKeys = keyof TargetType;
```

## Behavior with Object Types

When applied to a standard object type or interface, `keyof` extracts the explicitly defined property keys as a union of string, number, or symbol literals.

```typescript theme={"dark"}
const statusSymbol: unique symbol = Symbol();

interface User {
  id: number;
  username: string;
  [statusSymbol]: boolean;
}

type UserKeys = keyof User; 
// Resolves to: "id" | "username" | typeof statusSymbol
```

## Behavior with Index Signatures

If the target type contains an index signature, `keyof` resolves to the primitive types representing those dictionary keys. Because JavaScript implicitly coerces numeric object keys to strings, a string index signature yields a union of `string | number`.

```typescript theme={"dark"}
type StringDictionary = { [key: string]: unknown };
type StringDictKeys = keyof StringDictionary; 
// Resolves to: string | number

type NumberDictionary = { [key: number]: unknown };
type NumberDictKeys = keyof NumberDictionary; 
// Resolves to: number

type SymbolDictionary = { [key: symbol]: unknown };
type SymbolDictKeys = keyof SymbolDictionary;
// Resolves to: symbol
```

## The `keyof any` Idiom

Applying `keyof` to the `any` type yields the union of all possible property key types in JavaScript. This resolves to the base primitives allowed as object keys.

```typescript theme={"dark"}
type AllKeys = keyof any;
// Resolves to: string | number | symbol
```

## Behavior with Union and Intersection Types

The `keyof` operator applies specific algebraic rules when interacting with union (`|`) and intersection (`&`) types.

**Union Types:** `keyof` produces the *intersection* of the keys. It only extracts keys that are guaranteed to exist across all constituents of the union type.

```typescript theme={"dark"}
type A = { x: string; y: number };
type B = { y: number; z: boolean };

type UnionKeys = keyof (A | B); 
// Resolves to: "y"
```

**Intersection Types:** `keyof` produces the *union* of the keys. It extracts all keys available in the combined type.

```typescript theme={"dark"}
type IntersectionKeys = keyof (A & B); 
// Resolves to: "x" | "y" | "z"
```

## Behavior with Primitives and Arrays

When applied to primitive types, arrays, or tuples, `keyof` extracts the keys of the underlying JavaScript prototype, including methods and indices. Because strings can be indexed numerically (e.g., `str[0]`), `keyof string` includes the `number` primitive type.

```typescript theme={"dark"}
type StringKeys = keyof string;
// Resolves to: number | "length" | "concat" | "indexOf" | "slice" | ... (indices and String.prototype members)

type TupleKeys = keyof [string, number];
// Resolves to: "0" | "1" | "length" | "push" | "pop" | ... (indices and Array.prototype members)
```

## Interaction with `typeof`

Because `keyof` operates strictly on types, it cannot be applied directly to runtime variables. To extract keys from a runtime value, `keyof` must be chained with the `typeof` type query operator, which first resolves the value into a TypeScript type.

```typescript theme={"dark"}
const config = { port: 8080, host: "localhost" };

// Invalid: keyof requires a type, not a value
// type Keys = keyof config; 

// Valid: typeof converts the value to a type first
type ConfigKeys = keyof typeof config; 
// Resolves to: "port" | "host"
```

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