type Result<T> = { ok: true; value: T } | { ok: false; error: string }
function isOk<T>(r: Result<T>): r is { ok: true; value: T } {
return r.ok === true
}Discriminating on a boolean literal property is a clean way to narrow Result-style union types.
Challenge: Implement narrowUnion<T, E extends Error>(result: T | E): [T, null] | [null, E] β splitting a "result or error" union into a typed tuple.
narrowUnion('success') // β ['success', null]
narrowUnion(new Error()) // β [null, Error]