ProgramGuru

The keyof Type Operator

The keyof Type Operator — the core idea

The keyof operator computes a union of the permitted property names of a type. It is especially useful for a getter that must reject misspelled keys before the program runs. The key is a type-level constraint, while bracket access is ordinary JavaScript.

What you’ll learn

  • Explain the keyof type operator with a concrete use case.
  • Run the example and account for every line of its output.
  • Recognize the difference between compile-time checking and runtime behavior.

Understand the rules and when to use them

Reading the code: keyof Product is "sku" | "price". K can only be one of those keys. T[K] preserves the corresponding return type, so the first call is inferred as string and the second as number.

Keep the boundary clear: keyof produces possible keys, not an array you can loop over. Use Object.keys for actual runtime enumeration.

See the idea as a flow

The keyof Type Operator — conceptual flowProduct then keyof → "sku" | "price" then get(product, key)The keyof Type Operator1Product2keyof → "sku" | "price"3get(product, key)Type-level descriptions are erased; runtime actions need actual JavaScript.
Follow the three stages and identify what happens in TypeScript versus JavaScript.

Run the complete TypeScript example

Save the code as keyof-type-operator.ts. The result below was captured after compiling this exact example with strict TypeScript checking and executing the emitted JavaScript in Node.js.

TypeScript code

type Product = { sku: string; price: number };
function get<T, K extends keyof T>(item: T, key: K): T[K] {
  return item[key];
}
const product: Product = { sku: "BK-1", price: 120 };
console.log(get(product, "sku"));
console.log(get(product, "price"));

Verified output

BK-1
120

Build: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable keyof-type-operator.ts; run: node keyof-type-operator.js. The exact filename may be changed freely. When a multi-file project exists, prefer its tsconfig.json and build scripts.

Trace the example line by line

keyof Product is "sku" | "price". K can only be one of those keys. T[K] preserves the corresponding return type, so the first call is inferred as string and the second as number.

The output shown above comes from the initialized values in this program. Replacing a type annotation alone does not transform a JavaScript value.

Avoid this common mistake

keyof produces possible keys, not an array you can loop over. Use Object.keys for actual runtime enumeration.

Check both the TypeScript diagnostic and the emitted JavaScript behavior. A program can compile correctly and still need runtime validation or error handling.

Practice independently

Try get(product, "stock") and explain the compiler error. Add a stock field and retry.

Before running your change, write down the type you expect TypeScript to infer and the output you expect JavaScript to print. Then check both predictions.

Check your understanding

What is keyof Product in this example?
Choose one answer. You can try again.

Answer and explanation

It is the type union "sku" | "price".

keyof Product is "sku" | "price". K can only be one of those keys. T[K] preserves the corresponding return type, so the first call is inferred as string and the second as number.

What to remember

The keyof operator computes a union of the permitted property names of a type.

Try the practice change and re-run the exact program to test your understanding.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: The typeof Type Operator →