function processResult(result: Result<User, Error>) { if (result.isOk()) { // TypeScript narrows the type to Ok<User, Error> const user = result.value // Type: User console.log(user.name) } else { // TypeScript narrows the type to Err<User, Error> const error = result.error // Type: Error console.error(error.message) }}
function getUserName(result: Result<User, string>): string { if (!result.isOk()) { return 'Anonymous' } // TypeScript knows result is Ok here return result.value.name}
// Using isOk with conditional logicconst value = result.isOk() ? result.value : defaultValue// Using unwrapOr - more conciseconst value = result.unwrapOr(defaultValue)