keyof and typeof
Keys of T
keyof collects the property names of an object type into a union of literals. It is the typesafe way to talk about keys.
UserKey is name or age, so only real fields are accepted. A typo becomes a compile error instead of undefined at runtime.
type User = { name: string; age: number; }; type UserKey = keyof User; const key: UserKey = "name"; console.log(key);
name
Types from values
typeof lifts a value into the type world, capturing the shape TypeScript inferred for it. No hand-written duplicate is needed.
Settings mirrors the settings object exactly, including the number type of pageSize. Change the value and the type follows.
const settings = { theme: "dark", pageSize: 20, }; type Settings = typeof settings; const copy: Settings = { theme: "light", pageSize: 10 }; console.log(copy.theme, copy.pageSize);
light 10
Indexed access T[K]
Indexed access reads one property type out with square brackets. User of name is string, just like field access but for types.
Unions of keys work too, so name or age gives string or number. This pairs well with keyof for fully derived lookups.
type User = { name: string; age: number }; type Name = User["name"]; type Either = User["name" | "age"]; const name: Name = "Ada"; const either: Either = 36; console.log(name, either);
Ada 36
Config objects
Config objects tie the three ideas together. Functions accept the derived key and value types instead of loose strings.
getUserField returns the right value type for any valid key, and getPageSize reads the settings shape. Callers get autocomplete for free.
type User = { name: string; age: number }; type UserKey = keyof User; const settings = { theme: "dark", pageSize: 20 }; type Settings = typeof settings; function getUserField(user: User, key: UserKey): string | number { return user[key]; } function getPageSize(config: Settings): number { return config.pageSize; } const user: User = { name: "Ada", age: 36 }; console.log(getUserField(user, "age")); console.log(getPageSize(settings));
36
20
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
keyof Usergive for an object type?Answer
A union of its property names as literals
keyof reads the names of the properties, so a typo among them fails to compile.
-
What does
typeof settingsdo in a type position?Answer
It takes the type TypeScript inferred for the value
The value already has an inferred type, and typeof lifts that type into the type world.
-
What is
User["name"]fortype User = { name: string; age: number }?Answer
stringAn indexed access reads the type of one property, which is string here.
-
What is
User["name" | "age"]?Answer
string | numberEach key contributes its property type, and the results form a union.
-
What does this program print?
type User = { name: string; age: number }; type UserKey = keyof User; const key: UserKey = "name"; console.log(key);Answer
name
The constant holds the key name, and the log prints it.
-
What does this program print?
const settings = { theme: "dark", pageSize: 20, }; type Settings = typeof settings; const copy: Settings = { theme: "light", pageSize: 10 }; console.log(copy.theme, copy.pageSize);Answer
light 10
The copy satisfies the derived type with its own values, and the log prints them.
-
What does this program print?
type User = { name: string; age: number }; type Name = User["name"]; type Either = User["name" | "age"]; const name: Name = "Ada"; const either: Either = 36; console.log(name, either);Answer
Ada 36
Name is string and Either is string or number, so both constants fit and print.
-
What does this program print?
type User = { name: string; age: number }; type UserKey = keyof User; const settings = { theme: "dark", pageSize: 20 }; type Settings = typeof settings; function getUserField(user: User, key: UserKey): string | number { return user[key]; } function getPageSize(config: Settings): number { return config.pageSize; } const user: User = { name: "Ada", age: 36 }; console.log(getUserField(user, "age")); console.log(getPageSize(settings));Answer
36 / 20
The age field holds 36 and the settings pageSize holds 20.
-
What does the compiler say about this program?
type User = { name: string; age: number }; type UserKey = keyof User; const k: UserKey = "email"; console.log(k);Answer
error TS2322: Type '"email"' is not assignable to type 'keyof User'.
keyof User is name or age, and email is neither of them.
-
What does the compiler say about this program?
type User = { name: string; age: number }; type X = typeof User; const x: X = { name: "Ada", age: 1 }; console.log(x);Answer
error TS2693: 'User' only refers to a type, but is being used as a value here.
typeof reads a value, and a type alias such as User has no value to read.
Article author: Arthur Isaev