Object types, optional/readonly, index signatures
Define object types with properties, mark some as optional or readonly, and use index signatures for flexible keys.
Object types, optional/readonly, index signatures is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro
Goal: Define structured objects in TypeScript. You will explore object types, optional properties, readonly modifiers, and index signatures for dynamic keys.
Basic object
Object types define property names and their types. Every object must satisfy the declared structure.
type User = {
id: number;
name: string;
};
const u: User = { id: 1, name: "Ada" };
console.log(u);Optional property
Optional properties use "?". Objects may omit them, and TypeScript checks for undefined before use.
type User = {
id: number;
name: string;
email?: string; // optional
};
const a: User = { id: 1, name: "Ada" };
const b: User = { id: 2, name: "Lin", email: "l@example.com" };Readonly property
readonly ensures properties cannot be reassigned after initialization. Good for constants and config objects.
type Config = {
readonly version: number;
name: string;
};
const cfg: Config = { version: 1, name: "App" };
// cfg.version = 2; // Error, readonlyIndex signature
Index signatures allow unknown property names of a certain type. Example: keys are strings, values are numbers.
type Dictionary = {
[key: string]: number;
};
const scores: Dictionary = { math: 95, eng: 88 };
console.log(scores["math"]);Combining concepts
You can combine readonly, optional, and index signatures in the same type for flexible but safe models.
type Settings = {
readonly id: string;
theme?: "light" | "dark";
[extra: string]: string | number | undefined;
};
const s: Settings = { id: "abc", theme: "dark", fontSize: 14 };
console.log(s);Optional property check
Quick check: What does "?" after a property mean?
Recap
Recap: Objects can declare required, optional, readonly, and index-signature properties. These tools balance flexibility with safety.
Frequently asked questions
Is the “Object types, optional/readonly, index signatures” lesson free?
Yes — the full text of “Object types, optional/readonly, index signatures” is free to read here on the web, and the TypeScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Object types, optional/readonly, index signatures”?
Define object types with properties, mark some as optional or readonly, and use index signatures for flexible keys. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Object types, optional/readonly, index signatures” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Object types, optional/readonly, index signatures
- Interfaces vs Type Aliases
- Structural typing & excess property checks