JS Built-ins
TypeScriptintermediate

Utility Types β€” Required<T>

πŸ“– Usage Example
type Config = Required<{ host?: string; port?: number }>
// Config = { host: string; port: number }  (no more optional)

Required<T> removes optional markers from all properties, making them all mandatory.

Utility Types: Required<T>

Required<T> makes every optional property mandatory.

Challenge: Implement requireAll<T extends object>(obj: T): Required<T> β€” since properties can be undefined in JS, this function should throw if any own value is undefined.

requireAll({ a: 1, b: 2 })      // β†’ { a: 1, b: 2 } βœ…
requireAll({ a: 1, b: undefined }) // throws
πŸ’‘ Hints (1)
  • β€’ Check each property for undefined and throw with a descriptive message.