KoderSolutionKoderSolution LogoKoderSolution Logo
HomeArticlesTutorialsToolsRun CodeForumAI Lab
đź”·

TypeScript

Topic Hub & Articles

TypeScript Introduction

10 min

TypeScript Getting Started

10 min

TypeScript Simple Types

10 min

TypeScript Explicit & Inference

10 min

TypeScript Special Types

10 min

TypeScript Arrays

10 min

TypeScript Tuples

10 min

TypeScript Object Types

10 min

TypeScript Enums

10 min

TypeScript Aliases & Interfaces

10 min

TypeScript Union Types

10 min

TypeScript Functions

10 min

TypeScript Type Casting

10 min

TypeScript Classes

10 min

TypeScript Generics

10 min

TypeScript Utility Types

10 min

TypeScript `keyof` Operator

10 min

TypeScript Null & Undefined

10 min

TypeScript Definitely Typed

10 min

TypeScript 5 Updates

10 min

TypeScript Configuration

10 min

TypeScript Tooling

10 min

TypeScript Advanced Types

10 min

TypeScript Type Guards

10 min

TypeScript Conditional Types

10 min

TypeScript Mapped Types

10 min

TypeScript Type Inference

10 min

TypeScript Literal Types

10 min

TypeScript Namespaces

10 min

TypeScript Index Signatures

10 min

TypeScript Declaration Merging

10 min

TypeScript with Node.js

10 min

TypeScript with React

10 min

TypeScript Async Programming

10 min

TypeScript Decorators

10 min

TypeScript in JS Projects

10 min

TypeScript Migration

10 min

TypeScript Error Handling

10 min

TypeScript Best Practices

10 min

Progress
0%

0 / 39 Lessons

KoderSolutionKoderSolution LogoKoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Facebook PageX (Twitter) ProfileYouTube ChannelContact Support

Engineering Newsletter

Subscribe for weekly software engineering tutorials, architecture guides, and developer tool updates.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
TypeScriptTypeScript Types
Lesson

TypeScript `keyof` Operator

10 min reading
Free Course

TypeScript keyof: Type-Safe Dynamic Property Lookups

The keyof operator extracts a union of all public property keys from an object type. For an interface Product with sku, title, and price, keyof Product produces "sku" | "title" | "price".

Pairing keyof with generics (<T, K extends keyof T>) lets you build dynamic getters, sorters, and form binders where misspelled field names fail immediately at compile time.

Combining keyof with Indexed Access (T[K])

Indexed access T[K] looks up the exact value type stored at key K, preserving whether a field is string, number, or boolean.

interface Product {
  sku: string;
  title: string;
  price: number;
}

function pluckField<T, K extends keyof T>(items: readonly T[], key: K): T[K][] {
  return items.map((item) => item[key]);
}

const catalog: Product[] = [
  { sku: "KB-01", title: "Keyboard", price: 85 },
  { sku: "MS-02", title: "Mouse", price: 40 },
];

const prices = pluckField(catalog, "price"); // Inferred as number[]
console.log(prices);

keyof typeof on Value Objects

keyof works only on types, not runtime JavaScript objects. To extract keys from a runtime object like const config = { host: "localhost", port: 5432 }, write keyof typeof config.

flowchart LR
    A["const cfg = { host, port }"] -->|"typeof cfg"| B["{ host: string; port: number }"]
    B -->|"keyof ..."| C["'host' | 'port'"]

Common Mistakes to Avoid

  • Writing keyof myObject on a variable instead of a type: myObject is a runtime value; always write keyof typeof myObject.
  • Ignoring number | symbol in keyof keys: If an interface has a string index signature { [k: string]: unknown }, keyof evaluates to string | number because JavaScript coerces numeric keys (obj[0]) to "0".
  • Using Object.keys(obj) expecting (keyof T)[]: Object.keys() intentionally returns string[] because objects at runtime may carry extra keys beyond static types.

Frequently Asked Questions

What does keyof any evaluate to?

keyof any equals string | number | symbol, representing every valid JavaScript object property key type.

How do I restrict keys to only string keys in keyof T?

Use Extract<keyof T, string> (or keyof T & string) when passing keys to template literals or string-only functions.

Why does Object.keys(obj) not return (keyof typeof obj)[]?

Because TypeScript uses structural typing, an object variable can legally hold additional runtime properties not listed on its interface.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground

Related Developer Tools & Topic Hub

TypeScript Topic Hub
JSON Formatter
Validate, format, and minify JSON
Regex Tester
Test regular expression patterns
JWT Decoder
Inspect JSON Web Token payloads
All TypeScript Articles
Deep-dive guides, tips & discussions
PreviousTypeScript Utility Types
Continue JourneyNext: TypeScript Null & Undefined

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum