result-type-ts
v2.2.0
Published
A TypeScript library for the Result type
Downloads
4,980
Maintainers
Readme
A modern TypeScript library for the so-called Result type.
Features
- 0 dependencies
- Provides many utility functions about the Result type
- Well-tested
- Works on both browsers and Node.js
- Strict type inference
Usage
Here's a simple example of how to use this library:
import { Result } from 'result-type-ts'
const result: Result<number, string> = Math.random() < 0.5 ? Result.success(123) : Result.failure('error')
if (result.isSuccess) {
console.log(result.value) // 123
} else {
console.log(result.error) // error
}
API
Functions
Example
const result = Result.success(123)
console.log(result.value) // 123
console.log(result.isSuccess) // true
console.log(result.isFailure) // false
Example
const result = Result.failure('error')
console.log(result.error) // error
console.log(result.isFailure) // true
console.log(result.isSuccess) // false
Example
const result = Result.tryCatch(() => 123)
console.log(result.value) // 123
const result2 = Result.tryCatch(() => {
throw 'error'
})
console.log(result2.error) // error
Example
const result = Result.fromNullish(123);
console.log(result.value) // 123
const result2 = Result.fromNullish(null);
console.log(result2.error) // null
console.log(result2.isFailure) // true
Example
const result = await Result.fromPromise(Promise.resolve(123))
console.log(result.value) // 123
const result2 = await Result.fromPromise(Promise.reject('error'))
console.log(result2.error) // error
Example
const result = await Result.all([Result.success(123), Result.success(456)])
console.log(result.value) // 123
const result2 = await Result.all([Result.success(123), Result.failure('error')])
console.log(result2.error) // error
const result3 = await Result.all([Result.failure('error'), Result.failure('error2')])
console.log(result3.error) // error
Types
Example
const result: Result.Success<number> = Result.success(123)
Example
const result: Result.Failure<string> = Result.failure('error')
Example
const result: Result<number, string> = Math.random() > 0.5 ? Result.success(123) : Result.failure('error')
Properties
Example
const result = Result.success(123)
console.log(result.value) // 123
const result2 = Result.failure('error')
console.log(result2.value) // undefined
Example
const result = Result.success(123)
console.log(result.error) // undefined
const result2 = Result.failure('error')
console.log(result2.error) // error
Example
const result = Result.success(123)
console.log(result.isSuccess) // true
const result2 = Result.failure('error')
console.log(result2.isSuccess) // false
Example
const result = Result.success(123)
console.log(result.isFailure) // false
const result2 = Result.failure('error')
console.log(result2.isFailure) // true
Methods
Example
const result = Result.success(123)
console.log(result.getOrThrow()) // 123
const result2 = Result.failure('error')
try {
result2.getOrThrow()
} catch (e) {
console.log(e) // error
}
Example
const result = Result.success(123)
console.log(result.toUnion()) // 123
const result2 = Result.failure('error')
console.log(result2.toUnion()) // error
Example
const result = Result.success(123)
console.log(result.ifSuccess((value) => value * 2)) // 246
const result2 = Result.failure('error')
console.log(result2.ifSuccess((value) => value * 2)) // undefined
Example
const result = Result.success(123)
console.log(result.ifFailure((error) => error + '!')) // undefined
const result2 = Result.failure('error')
console.log(result2.ifFailure((error) => error + '!')) // error!
Example
const result = Result.success(123)
console.log(result.match((value) => value * 2, (error) => error + '!')) // 246
const result2 = Result.failure('error')
console.log(result2.match((value) => value * 2, (error) => error + '!')) // error!
Example
const result = Result.success(123).map((value) => value * 2)
console.log(result.value) // 246
const result2 = Result.failure('error').map((value) => value * 2)
console.log(result2.error) // error
Example
const result = Result.success(123).mapError((error) => error + '!')
console.log(result.value) // 123
const result2 = Result.failure('error').mapError((error) => error + '!')
console.log(result2.error) // error!
Example
const result = Result.success(123).flatMap((value) => Result.success(value * 2))
console.log(result.value) // 246
const result2 = Result.failure('error').flatMap((value) => Result.failure(value * 2))
console.log(result2.error) // error
Example
const result = await Result.success(123).flatMapAsync(async (value) => Result.success(value * 2))
console.log(result.value) // 246
const result2 = await Result.failure('error').flatMapAsync(async (value) => Result.failure(value * 2))
console.log(result2.error) // error
Example
const result = Result.success(Result.success(123)).flatten()
console.log(result.value) // 246
const result2 = Result.success(Result.failure('error')).flatten()
console.log(result2.error) // error
const result3 = Result.failure('error').flatten()
console.log(result3.error) // error
Example
const result: Result<number, Error> = Result.tryCatch(() => {
if (Math.random() >= 0) {
throw new Error('error')
} else {
return 123
}
}).assertErrorInstanceOf(Error)
console.log(result.isFailure && result.error.message) // error