The keyof Type Operator
Get a union of an object type's keys with keyof.
The keyof Type Operator is a free TypeScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Keys of a Type
The keyof operator takes an object type and produces a union of its property keys. It is the foundation for type-safe property access and generic key utilities.
Basic keyof
Apply keyof to an interface to get a union of its key names as string literal types.
interface User {
id: number
name: string
}
type UserKeys = keyof User // "id" | "name"
const k: UserKeys = "name"
console.log(k)Only Valid Keys Allowed
A variable typed as keyof T can only hold one of T's actual keys. Typos and invalid keys are caught at compile time.
interface User { id: number; name: string }
const valid: keyof User = "id"
// const bad: keyof User = "email" // Error: not a key of User
console.log(valid)keyof With typeof
Combine with typeof to get the keys of a value's inferred type. Here we get the keys of a config object without writing an interface.
const config = { host: "localhost", port: 8080 }
type ConfigKey = keyof typeof config // "host" | "port"
const key: ConfigKey = "port"
console.log(config[key])keyof of a String Index Signature
For a type with a string index signature, keyof is string | number — because any string key is allowed, and numeric keys coerce to strings.
type Dict = { [key: string]: number }
type DictKeys = keyof Dict // string | number
const k: DictKeys = "anything"
console.log(k)keyof of a Number Index Signature
For a number index signature, keyof is just number. This matches how arrays expose numeric indices.
type NumDict = { [i: number]: string }
type NumKeys = keyof NumDict // number
const k: NumKeys = 0
console.log(k)keyof an Array Type
Applying keyof to an array type yields a surprising union: numeric indices plus all array method and property names like length and push.
type ArrKeys = keyof string[]
// number | "length" | "push" | "map" | ... many more
const k: ArrKeys = "length"
console.log(k)Iterating Keys Safely
Use keyof to write loops that only touch valid keys. Casting Object.keys to (keyof T)[] keeps indexed access type-safe.
interface User { id: number; name: string }
const u: User = { id: 1, name: "Ada" }
const keys = Object.keys(u) as (keyof User)[]
for (const k of keys) console.log(k, u[k])keyof in Generic Constraints
A generic key parameter is usually constrained with K extends keyof T. This guarantees the key actually exists on the object.
function pluck<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key]
}
const name = pluck({ id: 1, name: "Ada" }, "name")
console.log(name) // AdaCombining Multiple Keys
Because keyof T is a union, you can intersect or filter it. Here a function accepts an array of valid keys.
function pick<T, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> {
const out = {} as Pick<T, K>
for (const k of keys) out[k] = obj[k]
return out
}
console.log(pick({ a: 1, b: 2, c: 3 }, ["a", "c"]))Why keyof Matters
keyof turns property names into a checkable type, enabling generic getters, setters, and mappers that cannot reference a key that does not exist. It is everywhere in advanced TypeScript.
interface Settings { theme: string; size: number }
function has<T>(obj: T, key: keyof T): boolean {
return key in (obj as object)
}
console.log(has<Settings>({ theme: "x", size: 1 }, "theme"))Quick Check
Test your understanding of the keyof operator.
Recap
keyof T produces a union of T's property keys as literal types, restricting variables to valid keys. It returns string | number for string index signatures and number for number index signatures. Paired with typeof, it derives keys from values; paired with generics (K extends keyof T), it powers type-safe getters, pickers, and iteration.
Frequently asked questions
Is the “The keyof Type Operator” lesson free?
Yes — the full text of “The keyof Type Operator” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “The keyof Type Operator”?
Get a union of an object type's keys with keyof. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The keyof Type Operator” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The typeof Type Operator
- The keyof Type Operator
- Indexed Access Types
- Combining typeof and keyof