Object Types: Optional, Readonly & Index Signatures
An object type describes the shape of an object — its properties and their types — with modifiers like ? for optional fields, readonly for fixed ones, and index signatures for dictionary-like keys.
Learn Object Types: Optional, Readonly & Index Signatures in our free TypeScript course — an interactive lesson with runnable examples, a practice exercise…
Part of the free TypeScript course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
An object type is a form to fill in . Some fields are required (name, price); some are optional (nickname — leave it blank); some are read-only like a printed reference number you can't change. An index signature is the difference between a fixed form and a blank ledger: a ledger lets you add any row label you like, as long as every entry is a number.
1. Optional ? & readonly
Add ? after a property name to make it optional — the field may be absent, so its type includes undefined and you must check before using it. Prefix a property with readonly to forbid reassignment after the object is created, which is perfect for IDs, timestamps, and other values that should never change.
2. Index Signatures
An index signature describes an object whose keys aren't known in advance but whose values share a type — exactly like a dictionary. You write it as {' '} , meaning "any string key maps to a number". You can even combine fixed properties with an index signature in the same type, as long as the fixed ones are compatible with the index value type.
3. Nested Object Types & Excess Property Checks
Object types nest : a property can itself be an object type, or an array of object types, to any depth. This is how you model real data like an order containing a customer with an address and a list of line items. TypeScript also runs an excess property check when you assign an object literal directly — flagging any extra field that isn't in the type, which catches typos before they ship.
🎯 Your Turn
A product has a required name and price plus an optional discount . Fill in the two blanks marked ___ , then run it.
No blanks this time — just a brief and a starting outline. Build the index-signature helpers yourself, run it, and check your output against the example in the comments.
Practice quiz
What does an object type describe?
- A class hierarchy
- A runtime value only
- The shape of an object: its properties and their types
- A CSS layout
Answer: The shape of an object: its properties and their types. An object type describes which properties an object has and the type of each.
What does the ? modifier on a property mean?
- The property is optional and may be absent
- The property is readonly
- The property is private
- The property must be null
Answer: The property is optional and may be absent. ? makes a property optional; its type becomes its type | undefined, so you must check before use.
Does readonly freeze the object at runtime?
- Yes, like Object.freeze
- Only in strict mode
- Only for numbers
- No, it is a compile-time guarantee only
Answer: No, it is a compile-time guarantee only. readonly blocks reassignment during type checking; for runtime immutability use Object.freeze().
What does the index signature { [player: string]: number } mean?
- Only a key named player is allowed
- Any string key maps to a number value
- Keys must be numbers
- The object is empty
Answer: Any string key maps to a number value. An index signature means any string key is allowed and each value must be a number.
When is an index signature the right tool?
- When key names are not known ahead of time but values share a type
- When you know every key in advance
- When you need a class
- When values differ wildly
Answer: When key names are not known ahead of time but values share a type. Use it for dictionaries and maps where keys are open but every value shares a type.
Can object types nest?
- No, only one level is allowed
- Only two levels
- Yes, a property can itself be an object type to any depth
- Only arrays can nest
Answer: Yes, a property can itself be an object type to any depth. Object types nest: a property can be another object type or an array of them, to any depth.
What is the excess property check?
- A check that arrays are short
- Flagging extra fields on a directly assigned object literal
- A runtime validation
- A check on function arguments
Answer: Flagging extra fields on a directly assigned object literal. Assigning an object literal directly flags any property not in the type, catching typos.
Why does the excess property check not fire when you assign through a variable first?
- A bug
- Variables disable checking
- Only literals have types
- TypeScript uses structural typing for non-literal assignments
Answer: TypeScript uses structural typing for non-literal assignments. The check only applies to fresh literals; via a variable, structural typing tolerates the extra field.
Which operator is recommended for defaulting an optional value?
- ||
- ??
- &&
- !
Answer: ??. Prefer ?? over || so a legitimate 0 or empty string is not replaced.
Which built-in utility reads more clearly than a raw index signature for open string keys?
- Array<T>
- Partial<T>
- Record<string, T>
- Map<T>
Answer: Record<string, T>. Record<string, T> is the built-in utility that expresses an open string-keyed dictionary clearly.