@schrosis/rus-ts v0.0.2
@schrosis/rus-ts
š¦ < Something like Rust with excellent type inference.
Install
npm install @schrosis/rus-tsor
yarn add @schrosis/rus-tsFeature
- Rust like Option type.
- Rust like Result type. (Coming soon)
- Zero dependency.
- No panic!
Usage
Option
Type Option represents an optional value: every Option is either Some and contains a value, or None, and does not. Option types are very common in Rust code, as they have a number of uses:
- Initial values
- Return value for otherwise reporting simple errors, where
Noneis returned on error - Optional struct fields
- Optional function arguments
- Swapping things out of difficult situations
Options are paired with pattern matching by if to query the presence of a value and take action, always accounting for the None case.
const divide = (numerator: number, denominator: number): Option<number> => {
if (denominator === 0) {
return None;
} else {
return Some(numerator / denominator);
}
};
// The return value of the function is an option
let result = divide(2.0, 3.0);
// Pattern match to retrieve the value
if (result.isSome()) {
// The division was valid
console.log(`Result: ${result.value}`);
} else {
console.log('Cannot divide by 0');
}Method overview
Option implements several methods in Rust Option.
Querying the variant
The isSome and isNone methods return true if the Option is Some or None, respectively.
Extracting the contained value
If Option is type inferred as Some, it can be accessed by this.value.
*TypeScript does not have the concept of panic, so references that result in a panic are not allowed.
Transforming contained values
These methods transform Option to Result:
okOrtransformsSome(v)toOk(v), andNonetoErr(err)using the provided defaulterrvalueokOrElsetransformsSome(v)toOk(v), andNoneto a value ofErrusing the provided functiontransposetransposes anOptionof aResultinto aResultof anOption
(Coming soon.)
These methods transform the Some variant:
filtercalls the provided predicate function on the contained valuetif theOptionisSome(t), and returnsSome(t)if the function returnstrue; otherwise, returnsNoneflattenremoves one level of nesting from anOption<Option<T>>maptransformsOption<T>toOption<U>by applying the provided function to the contained value ofSomeand leavingNonevalues unchanged
These methods transform Option<T> to a value of a possibly different type U:
mapOrapplies the provided function to the contained value ofSome, or returns the provided default value if theOptionisNonemapOrElseapplies the provided function to the contained value ofSome, or returns the result of evaluating the provided fallback function if theOptionisNone
These methods combine the Some variants of two Option values:
zipreturnsSome([s, o])if self isSome(s)and the providedOptionvalue isSome(o); otherwise, returnsNone
Boolean operators
These methods treat the Option as a boolean value, where Some acts like true and None acts like false. There are two categories of these methods: ones that take an Option as input, and ones that take a function as input (to be lazily evaluated).
The and, or, and xor methods take another Option as input, and produce an Option as output. Only the and method can produce an Option<U> value having a different inner type U than Option<T>.
| method | this | input | output |
|---|---|---|---|
and | None | (ignored) | None |
and | Some(x) | None | None |
and | Some(x) | Some(y) | Some(y) |
or | None | None | None |
or | None | Some(y) | Some(y) |
or | Some(x) | (ignored) | Some(x) |
xor | None | None | None |
xor | None | Some(y) | Some(y) |
xor | Some(x) | None | Some(x) |
xor | Some(x) | Some(y) | None |
The andThen and orElse methods take a function as input, and only evaluate the function when they need to produce a new value. Only the andThen method can produce an Option<U> value having a different inner type U than Option<T>.
| method | this | function input | function result | output |
|---|---|---|---|---|
andThen | None | (not provided) | (not evaluated) | None |
andThen | Some(x) | x | None | None |
andThen | Some(x) | x | Some(y) | Some(y) |
orElse | None | (not provided) | None | None |
orElse | None | (not provided) | Some(y) | Some(y) |
orElse | Some(x) | (not provided) | (not evaluated) | Some(x) |
Methods not in Rust
In TypeScript, the types T|undefined and T|null are usually used.
There are methods to generate Option from these types.
| method | input | output |
|---|---|---|
Option.fromUndefinable | T\|undefined | Option<T> |
Option.fromNullable | T\|null | Option<T> |
For more
š¦ < See the module level documentation for more.