Partial, Required, and Readonly
Partial, Required, and Readonly — the core idea
Partial<T> allows omitted properties; Required<T> requires declared properties; Readonly<T> prevents typed assignment through that view. They can be composed when editing a record in stages, but none of them performs runtime validation.
What you’ll learn
- Explain partial, required, and readonly with a concrete use case.
- Run the example and account for every line of its output.
- Recognize the difference between compile-time checking and runtime behavior.
Understand the rules and when to use them
Reading the code: changes may omit id. full supplies both id and nickname, satisfying Required. safe forbids writes through that reference during type checking; console.log reads its values normally.
Keep the boundary clear: Required does not fill in missing values automatically; it only reports errors if an assigned value omits required keys.
See the idea as a flow
Run the complete TypeScript example
Save the code as partial-required-readonly.ts. The result below was captured after compiling this exact example with strict TypeScript checking and executing the emitted JavaScript in Node.js.
TypeScript code
type Profile = { id: number; nickname?: string };
const changes: Partial<Profile> = { nickname: "Lin" };
const full: Required<Profile> = { id: 5, nickname: changes.nickname ?? "anonymous" };
const safe: Readonly<Profile> = full;
console.log(`${safe.id}:${safe.nickname}`);Verified output
5:LinBuild: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable partial-required-readonly.ts; run: node partial-required-readonly.js. The exact filename may be changed freely. When a multi-file project exists, prefer its tsconfig.json and build scripts.
Trace the example line by line
changes may omit id. full supplies both id and nickname, satisfying Required. safe forbids writes through that reference during type checking; console.log reads its values normally.
The output shown above comes from the initialized values in this program. Replacing a type annotation alone does not transform a JavaScript value.
Avoid this common mistake
Required does not fill in missing values automatically; it only reports errors if an assigned value omits required keys.
Check both the TypeScript diagnostic and the emitted JavaScript behavior. A program can compile correctly and still need runtime validation or error handling.
Practice independently
Make a Partial<Profile> containing only id, then construct a Required<Profile> with a default nickname.
Before running your change, write down the type you expect TypeScript to infer and the output you expect JavaScript to print. Then check both predictions.
Check your understanding
What to remember
Partial<T> allows omitted properties; Required<T> requires declared properties; Readonly<T> prevents typed assignment through that view.
Try the practice change and re-run the exact program to test your understanding.
Reaching the recap and completing the quizzes are separate milestones.