Sub topic 4
Keys of a Runtime Object: `keyof typeof`
This chapter assumes you know basic TypeScript interfaces, type aliases, union types, JavaScript objects, and generic type parameters. You should also know that TypeScript checks types before JavaScript runs.
Why `keyof typeof` is useful
A runtime object has actual values. A type describes the shape that values should have. Sometimes you want to derive a type from an object that already exists instead of writing the same information twice.
typeof in a type position reads the type of a value. keyof then turns that object type into a union of its property names.
const config = {
host: "localhost",
port: 8080,
};
type Config = typeof config;
// { host: string; port: number }
type ConfigKey = keyof typeof config;
// "host" | "port"The expression config is a value. The expression typeof config is a type-level operation that describes that value. The expression keyof typeof config means:
- Read the type of
config. - Get the keys of that type.
- Produce the union
"host" | "port".
You can use the resulting type to restrict strings to valid configuration keys.
function readConfig(key: ConfigKey): string | number {
return config[key];
}
readConfig("host");
readConfig("port");
// readConfig("protocol"); // ErrorThe compiler rejects "protocol" because it is not a key of config.
Type positions and value positions
TypeScript has a value space and a type space. Values exist when the JavaScript program runs. Types exist only while TypeScript checks the program.
| Expression | Space | Meaning |
|---|---|---|
| `config` | Value | The object available at runtime |
| `typeof config` | Type | The inferred type of that object |
| `keyof typeof config` | Type | A union of the object's keys |
JavaScript also has a runtime typeof operator:
console.log(typeof config);
// "object"That operator produces a string while the program runs. The type-position form appears where TypeScript expects a type:
type Config = typeof config;These two uses have the same spelling but different jobs.
Keeping the object and key type together
Deriving the key type from the object prevents the object and its key union from drifting apart.
const endpoints = {
users: "/api/users",
posts: "/api/posts",
comments: "/api/comments",
};
type EndpointName = keyof typeof endpoints;
function getEndpoint(name: EndpointName): string {
return endpoints[name];
}
console.log(getEndpoint("users"));
console.log(getEndpoint("posts"));If you add a property, the type updates automatically:
endpoints.health = "/health";With normal object inference, this new property is reflected in keyof typeof endpoints when TypeScript checks the surrounding code. If you remove a property, the corresponding key is removed from the derived type.
This pattern is especially useful for registries, route tables, feature flags, localization dictionaries, and configuration objects.
Literal keys versus literal values
keyof typeof preserves the property names as literal types, but normal object inference usually widens property values:
const settings = {
mode: "dark",
};
type SettingKey = keyof typeof settings;
// "mode"
type Setting = typeof settings;
// { mode: string }The key remains the literal type "mode". The value type becomes string, rather than the literal type "dark". If the exact value matters, as const can preserve it:
const settings = {
mode: "dark",
} as const;
type Mode = typeof settings.mode;
// "dark"The key operation is still keyof typeof settings; as const controls how precisely the values are inferred.
Exercise
Given const colors = { primary: "#06c", danger: "#c00" }, write a type named ColorName that accepts only the object's property names.
Show answer
type ColorName = keyof typeof colors;