EasyJavaScriptTypeScript

Utility Types — Partial & Required

TypeScriptUtility TypesGenerics

Use TypeScript's built-in utility types to implement two config-update helpers.

1. `applyDefaults<T>(defaults: T, overrides: Partial<T>): T`
Merges overrides into defaults. Properties in overrides overwrite defaults.

2. `requireAll<T>(config: Partial<T>): config is Required<T>`
Returns true if every value in the config is not undefined.

Examples

  • applyDefaults({ host: 'localhost', port: 3000 }, { port: 8080 }){ host: 'localhost', port: 8080 }
  • requireAll({ a: 1, b: 2 })true
  • requireAll({ a: 1, b: undefined })false

Sample tests

Test #1applyDefaults — override port
Input: ["applyDefaults",{"host":"localhost","port":3000},{"port":8080}]
Output: {"host":"localhost","port":8080}
Test #2applyDefaults — empty overrides
Input: ["applyDefaults",{"host":"localhost","port":3000},{}]
Output: {"host":"localhost","port":3000}
Test #3requireAll — all defined
Input: ["requireAll",{"a":1,"b":2}]
Output: true
Test #4requireAll — empty object (vacuously true)
Input: ["requireAll",{}]
Output: true