0 of 6 problems solved0%
6 of 6

Alias a Result Type

A common pattern is a result that can be OK or an error. Aliasing each variant and the union keeps function signatures tight and readable. Narrow by the discriminant to decide which fields are available.

  • Create two object aliases with a shared literal property.
  • Create a union alias combining them.
  • Return formatted strings consistently.

Your Task

  1. Define aliases:
  • type Ok = { ok: true; value: string }
  • type Fail = { ok: false; error: string }
  1. Define type Result = Ok | Fail.
  2. Implement summarize(r: Result): string:
  • "OK: <value>" when ok: true
  • "ERROR: <error>" when ok: false.