Sub topic 13
Pitfalls: Object.keys and Runtime Extra Properties
The prerequisite and the problem
Assume you know basic TypeScript type annotations, interfaces, type aliases, and JavaScript objects. This chapter focuses on a boundary between TypeScript's compile-time model and JavaScript's runtime behavior.
TypeScript uses structural typing. If an object has the required properties of Point, TypeScript can use it as a Point even if it has additional properties:
interface Point {
x: number;
y: number;
}
const withLabel = {
x: 3,
y: 4,
label: "A"
};
const point: Point = withLabel; // OKThe variable point has the static type Point, but the runtime object still contains label. TypeScript does not remove properties when it assigns a value to a narrower type.
Why Object.keys returns string[]
Object.keys is a JavaScript function that returns the enumerable own property names of an object at runtime. Its TypeScript return type is string[], not (keyof Point)[].
That result is deliberate. A value typed as Point can contain properties that are not declared by Point. If TypeScript returned only "x" | "y", it would make a promise that the runtime object cannot guarantee.
interface Point {
x: number;
y: number;
}
const runtimePoint = {
x: 3,
y: 4,
label: "A"
};
const point: Point = runtimePoint;
for (const key of Object.keys(point)) {
console.log(key); // x, y, label
}
// const value = point[key];
// Error: a string cannot be used to index type 'Point'The expression keyof Point means the known property names of Point. In this example, it is "x" | "y". A general string might also be "label" or any other runtime property, so TypeScript does not allow it to index point safely.
Safe ways to iterate
When the operation is specifically about the declared fields of a type, keep an explicit list of those fields:
interface Point {
x: number;
y: number;
}
const runtimePoint = {
x: 3,
y: 4,
label: "A"
};
const point: Point = runtimePoint;
const pointKeys = ["x", "y"] as const;
let total = 0;
for (const key of pointKeys) {
total += point[key];
}
console.log(total); // 7The as const assertion preserves the array elements as the literal types "x" and "y" instead of widening them to string. Therefore, key is safe for indexing Point.
Use Object.keys when you need actual runtime keys. Use a typed key list when you need the declared keys of a known type.
| Goal | Suitable approach | Reason |
|---|---|---|
| Inspect every runtime property | `Object.keys(value)` | Includes extra enumerable properties |
| Iterate known fields | `const keys = [...] as const` | The list matches the type |
| Accept arbitrary keys | Validate each key first | A string is not automatically a valid property name |
Avoid unsafe casts
You may see this pattern:
const keys = Object.keys(point) as (keyof Point)[];The cast silences the compiler, but it does not check the data. If point contains label, the loop still visits "label". A cast can therefore hide a runtime bug, especially when the code assumes every value is a number.
This matters when data comes from JSON, an API, user input, or JavaScript code. TypeScript types describe what your program assumes. They do not validate external data or make runtime objects exact.
The working model
Keep these two views separate:
- The static view describes the properties the compiler knows about.
- The runtime view contains the properties the JavaScript object actually has.
- Structural assignability allows the runtime view to contain more properties.
Object.keysreports the runtime view, so its result is broader thankeyof.
Exercise
Why is Object.keys(point) typed as string[] when point has the static type Point?
Show answer
Because structural typing allows point to contain extra runtime properties, and Object.keys reports actual enumerable property names rather than only the properties declared by Point.