Object Types
_TypeScript · Reference cheat sheet_
Object Types
TypeScript · Reference cheat sheet
📖 Overview
Object types describe property names, optionality, readonly-ness, index signatures, and call/construct signatures. Use interface or type for named shapes; anonymous object types work inline.
🧩 Core concepts
- Properties — required by default;
?makes optional;readonlyprevents reassignment. - Index signatures —
[key: string]: T/[key: number]: Tfor dynamic keys. - Excess property checks — apply to fresh object literals assigned to typed targets.
- Intersection (
&) — combine shapes; conflicting properties becomeneverwhen incompatible. Record<K, V>— mapped object with keysKand valuesV.objectvs\{\}vsRecord<string, unknown>— prefer explicit shapes;objectmeans non-primitive.
💡 Examples
interface Product {
readonly id: string;
name: string;
price?: number;
}
const p: Product = { id: "sku-1", name: "Mug" };
// p.id = "x"; // error: readonly
type Dict = { [key: string]: number };
const scores: Dict = { alice: 10, bob: 8 };
// Nested + optional chaining-friendly shapes
type Config = {
server: { host: string; port: number };
features?: { darkMode?: boolean };
};
// Intersection
type Timestamped = { createdAt: Date };
type Entity = Product & Timestamped;
const item: Entity = {
id: "1",
name: "Tea",
createdAt: new Date(),
};
// Prefer explicit over empty object
type JsonObject = Record<string, unknown>;⚠️ Pitfalls
- Optional props may be missing or explicitly
undefineddepending onexactOptionalPropertyTypes. - String index signatures force all named props to be assignable to the index type.
\{\}accepts almost everything exceptnull/undefined— rarely what you want.- Methods vs function properties differ under
strictFunctionTypes/thistyping.