Sub topic 7
Inherited Keys and Null-Prototype Objects
Prerequisites
You should know JavaScript objects, bracket notation, TypeScript type annotations, and the difference between undefined and a missing property. This chapter assumes you already know basic index signatures.
An index signature describes the types of values you intend to store. It does not remove JavaScript's object prototype. An object typed as Record<string, number> can still inherit properties such as toString.
Why inherited keys matter
Consider a dictionary of scores:
const scores: Record<string, number> = {
alice: 90,
bob: 85,
};
console.log(scores["alice"]); // 90
console.log(scores["toString"]); // function toString() { ... }The type says that string keys have number values, but JavaScript still looks up inherited properties when a key is not an own property. This creates a mismatch. The type describes the dictionary entries, while the runtime object also has prototype entries.
This matters when you:
- Check whether a key exists.
- Iterate over user-provided keys.
- Accept dictionary keys from an external request.
- Store special names such as
constructoror__proto__.
`in` checks the whole property chain
The in operator returns true for an own property or an inherited property:
const lookup: Record<string, number> = { a: 1 };
console.log("a" in lookup); // true
console.log("toString" in lookup); // true
console.log("missing" in lookup); // falseBecause toString comes from Object.prototype, "toString" in lookup is true even though the dictionary never stored a value under that key.
Use in when inherited properties should count. For example, it can be appropriate when working with class instances and their methods.
`Object.hasOwn` checks dictionary entries
For a dictionary, you usually want to check only the object's own properties. Object.hasOwn does that:
const lookup: Record<string, number> = { a: 1 };
console.log(Object.hasOwn(lookup, "a")); // true
console.log(Object.hasOwn(lookup, "toString")); // false
console.log(Object.hasOwn(lookup, "missing")); // falseObject.hasOwn was added in ES2022. Configure TypeScript with an appropriate library target, such as "lib": ["ES2022"], when necessary.
After an existence check, TypeScript can narrow an indexed value when strict checking is enabled:
const prices: Record<string, number> = {
book: 12,
};
const key = "book";
if (Object.hasOwn(prices, key)) {
console.log(prices[key].toFixed(2));
}The check prevents your code from treating an inherited or missing property as a dictionary value.
Null-prototype objects
A null-prototype object has no prototype. Therefore, it has no inherited toString, constructor, or other Object.prototype properties:
const dictionary: Record<string, number> = Object.create(null);
dictionary["a"] = 1;
console.log("a" in dictionary); // true
console.log("toString" in dictionary); // false
console.log(Object.hasOwn(dictionary, "a")); // trueThe type annotation tells TypeScript how the object will be used. Object.create(null) changes the runtime object's prototype.
A null-prototype object is useful when arbitrary strings are data keys and inherited names could cause confusion. It is especially useful for low-level lookup tables and code that processes untrusted key names.
However, it does not have normal object methods:
const dictionary: Record<string, number> = Object.create(null);
dictionary["a"] = 1;
// dictionary.hasOwnProperty("a"); // Error at runtimeUse Object.hasOwn(dictionary, key) instead of calling dictionary.hasOwnProperty(key). This works for both ordinary objects and null-prototype objects.
How the checks differ
Choose Object.hasOwn for dictionary membership. Choose in when the prototype chain is part of the data model. A null-prototype object removes the usual prototype chain, but you should still use explicit checks because that makes the intent clear.
Exercise
Why does "toString" in lookup return true for an ordinary dictionary, and which check should you use to test whether the dictionary stored that key?
Show answer
It returns true because toString is inherited from Object.prototype. Use Object.hasOwn(lookup, "toString") to check only the dictionary's own entries.