interfaceSettings{
theme:"light"|"dark";
fontSize: number;
notifications: boolean;}functionupdateSettings(current:Settings, changes:Partial<Settings>):Settings{return{...current,...changes};}const updated =updateSettings({ theme:"light", fontSize:14, notifications:true},{ fontSize:16}// only need to pass what changed);
Output
// Partial<Settings> makes all properties optional
Note Partial only operates one level deep. Nested objects keep their original required types. For deep partial, you need a custom recursive type or a library utility.
Frequently asked questions
How does Python handle Optional?
This task is covered in 2 stacks on this page: Python, TypeScript. The "typing Module Essentials" snippet in Python uses `from typing import Optional, Union, TypeVar, Generic`.
Which code does the Python example use?
The "typing Module Essentials" snippet uses `from typing import Optional, Union, TypeVar, Generic`, from the Common Standard Library section of the Python cheat sheet.
Which stacks cover "Optional" on this page?
Python, TypeScript. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "typing Module Essentials": Since 3.10, use X | Y instead of Union[X, Y]. Since 3.12, use the type parameter syntax: class Stack[T]: instead of TypeVar + Generic.