Skip to main content

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.

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.
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.
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.
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.
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.
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.
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.
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.
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"
Master TypeScript with Deep Grasping Methodology!Learn More