rs-enums
v0.0.0
Published
rust-like enums for TypeScript
Downloads
94
Readme
➕ Installation
# yarn
yarn add rs-enums
# npm
npm install rs-enums
# pnpm
pnpm install rs-enums
✨ Usage
Opt<T>
import { Opt, Some, None } from 'rs-enums'
// generate Opt<T> with Some<T> and None<T>
const some = Some('hi')
const none = None<string>()
// Opt<T>#unwrap(): T
console.log(some.unwrap()) // 'hi'
none.unwrap() // throws an error because cannot unwrap None
Result<T, E>
import { Result, Ok, Err } from 'rs-enums'
// generates Result<T, E> with Ok<T, E> and Err<T, E>
const ok = Ok<string, string>('success')
const err = Err<string, string>('error')
console.log(ok.expect('should be ok')) // 'success'
err.expect('should be ok') // throws an error with message 'should be ok'