Object Types in TypeScript
Describe object shapes with inline types, interfaces, index signatures, and Record.
What you'll learn
- Inline object type literals vs named interfaces
- Optional, readonly, and excess property checking
- Index signatures for dynamic-key objects
- Using Record<K, V> for keyed maps
Concept
Object Type Literals
You can describe an object shape inline without naming it: let user: { id: number; name: string } = { id: 1, name: "Alice" }. Inline types are convenient for one-off shapes, but for reusable types you should name them with an interface or type alias. Inline types are common in function parameters: function log(user: { name: string }): void.
Index Signatures
When an object has dynamic keys (like a lookup table), use an index signature: { [key: string]: number } means every string key maps to a number. This is useful for caches, frequency maps, and configuration objects. The Record<K, V> utility type is a cleaner alternative: Record<string, number> is equivalent to { [key: string]: number } but more readable.
Property Modifiers
Optional properties (email?: string) may be absent. Readonly properties (readonly id: number) cannot be reassigned. TypeScript performs excess property checking on object literals — assigning { id: 1, name: "Alice", extra: true } to a type without extra is an error. This catches typos but can be bypassed by assigning through an intermediate variable.
Example
// Inline object type — good for one-off shapes.
let user: { id: number; name: string; email?: string } = {
id: 1,
name: "Alice",
};
// Index signature — dynamic string keys, all number values.
let scores: { [key: string]: number } = {
math: 90,
science: 85,
};
// Record<K, V> — cleaner syntax for keyed maps.
let config: Record<string, boolean> = {
debug: true,
verbose: false,
};
// Readonly and optional combined.
type Account = {
readonly id: number;
name: string;
readonly createdAt: Date;
};
const acc: Account = { id: 1, name: "Bob", createdAt: new Date() };
// acc.id = 2; // ❌ Error: readonly
console.log(user.name, scores.math, config.debug, acc.name);
Inline types work for one-off shapes. Index signatures { [key: string]: V } or Record<string, V> handle dynamic keys. Optional (?) and readonly modifiers refine properties.
Try it
- JSON to TypeScript
Generate object types and interfaces from JSON examples.
- JSON Validator
Validate that JSON matches your object type before using it.
Common mistakes
The mistake
Using an index signature when keys are actually known and finite
The fix
If you know all possible keys, list them explicitly as named properties. Index signatures lose key-level type safety and allow any string key.
The mistake
Excess property errors when spreading objects
The fix
Excess property checking applies to object literals. Spreading or assigning through a variable bypasses it. If the extra properties are intentional, widen the target type.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.