Partial and Required
Concept and syntax
Partial<T> makes the properties optional at one level; Required<T> removes optional modifiers at that level. Neither recursively transforms nested objects. A patch type is not a merge implementation, and undefined handling depends on the property declaration and compiler settings.
Working example
Save each snippet separately as main.ts in a fresh directory with TypeScript 5.8.3. Compile with tsc --strict --target ES2022 --module commonjs --moduleDetection force --skipLibCheck --noEmitOnError main.ts and run accepted snippets with node main.js. Type aliases are erased; assignments and diagnostic checks demonstrate the type relations, not the printed output alone.
type Config={count:number;label?:string};const patch:Partial<Config>={count:2}; const full:Required<Config>={count:2,label:"tea"}; console.log(patch.count,full.label);Expected output:
2 teaCommon mistake
Intentionally invalid; compile separately and do not execute.
type Config={count:number;label?:string};const full:Required<Config>={count:2};Expected diagnostic: TS2741. Required makes label mandatory.
Practice and recap
Add a nested required object and check whether Partial changes its children.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Partial and Required — knowledge check
Check the type transformation, trace output, diagnose a rejected example, and apply the concept to a new case.
Which statement about Partial and Required is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
type Config={count:number;label?:string};const patch:Partial<Config>={count:2}; const full:Required<Config>={count:2,label:"tea"}; console.log(patch.count,full.label);Which diagnostic and explanation match the rejected example under the lesson settings?
type Config={count:number;label?:string};const full:Required<Config>={count:2};For Partial<{nested:{n:number}}>, what happens to n if nested is supplied?