Model data and type relationships
How to Give TypeScript Interface Objects Runtime Defaults
Build complete interface-shaped settings with a typed defaults object and a factory that handles omitted, undefined, and falsy overrides.

An interface can describe the object you want, but it cannot create that object or fill in its properties. For a Settings object with defaults, define the values separately and use a factory to combine them with caller input. Interfaces describe compile-time structure, not runtime default-value logic (DevGex).
Define every required default
Suppose an application needs a service name, a retry limit, and a flag controlling notifications. All three should be present in the result, even when the caller supplies nothing. Keep them required in Settings, then put their actual values in a defaults object annotated as Settings.
That annotation lets the TypeScript checker assess whether the defaults object has the required properties with the declared types. It does not initialize any other object. A separately initialized defaults object is also shown in this interface-default example.
Marking retryLimit optional would answer a different question: it would allow an object to omit the property, not put 3 into it. Here, callers may omit an override, while the factory remains responsible for returning a complete object.
Accept partial input and return complete settings
This example keeps the configuration flat. SettingsOverrides makes each input property optional and explicitly allows undefined. The factory accepts an empty object by default and chooses each result property separately:
interface Settings {
serviceName: string;
retryLimit: number;
notificationsEnabled: boolean;
}
const defaults: Settings = {
serviceName: 'worker',
retryLimit: 3,
notificationsEnabled: false,
};
type SettingsOverrides = {
serviceName?: string | undefined;
retryLimit?: number | undefined;
notificationsEnabled?: boolean | undefined;
};
function createSettings(overrides: SettingsOverrides = {}): Settings {
return {
serviceName: overrides.serviceName !== undefined
? overrides.serviceName
: defaults.serviceName,
retryLimit: overrides.retryLimit !== undefined
? overrides.retryLimit
: defaults.retryLimit,
notificationsEnabled: overrides.notificationsEnabled !== undefined
? overrides.notificationsEnabled
: defaults.notificationsEnabled,
};
}
const standard = createSettings();
const forReports = createSettings({
serviceName: 'reports',
notificationsEnabled: true,
});
const withUndefined = createSettings({ retryLimit: undefined });
const deliberate = createSettings({
serviceName: '',
retryLimit: 0,
notificationsEnabled: false,
});
console.log(standard);
console.log(forReports);
console.log(withUndefined);
console.log(deliberate);
For these calls, the expected output is:
{ serviceName: 'worker', retryLimit: 3, notificationsEnabled: false }
{ serviceName: 'reports', retryLimit: 3, notificationsEnabled: true }
{ serviceName: 'worker', retryLimit: 3, notificationsEnabled: false }
{ serviceName: '', retryLimit: 0, notificationsEnabled: false }
The second call changes two properties without making the caller repeat retryLimit. The return type remains Settings; it is the values selected inside the function, not the return annotation alone, that make the result complete.
Make the override rule explicit
For this factory, an omitted property and a property explicitly set to undefined both select the default. Reading the omitted property yields undefined, and each !== undefined check chooses the corresponding fallback. The interface does not provide that rule. It comes from the executable checks, a technique also used for optional configuration values in this configuration example.
A { ...defaults, ...overrides } merge makes a different choice. Spreading defaults first and caller values afterward is a concise way to replace selected properties, as in Bobby Hadz’s Partial<Person> example. But if an override supplies retryLimit: undefined, that later property overwrites 3. Use the per-property checks when explicit undefined should mean “use the default.”
The same checks preserve intentional falsy values. 0, false, and '' are all different from undefined, so the deliberate call retains them. Zero and the empty string visibly differ from this example’s defaults. The TypeScript handbook’s configuration example uses truthiness checks for optional values; that kind of check would not express the rule used here.
Check the contract and the values separately
Type-check the complete example to examine the declared defaults, accepted calls, and Settings result. As a separate, deliberately invalid check, try createSettings({ retryLimit: 'many' }): a string does not match the declared number override. Remove that call before running the example.
Then inspect the four logged objects. They demonstrate what this factory selects for no input, ordinary overrides, explicit undefined, and falsy overrides. Neither the annotation nor those examples validates values arriving from an untyped source. If settings come from outside typed code, check their values before passing them as overrides; this factory only applies its defaulting rule.




