JS Built-ins
TypeScriptintermediate

Utility Types β€” withDefaults

πŸ“– Usage Example
withDefaults({ host: 'custom.com' }, { host: 'localhost', port: 3000 })
// β†’ { host: 'custom.com', port: 3000 }

withDefaults merges user options over defaults, filling in only properties that are not already set.

Utility Types: withDefaults using Required + Partial

Challenge: Implement withDefaults<T extends object>(input: Partial<T>, defaults: Required<T>): Required<T> filling missing fields from defaults.

withDefaults({ a: 1 }, { a: 10, b: 20 })  // β†’ { a: 1, b: 20 }
πŸ’‘ Hints (1)
  • β€’ Spread defaults first, then input β€” later spreads overwrite earlier ones.