https://github.com/zephraph/rust-types
Rust types for TypeScript
https://github.com/zephraph/rust-types
Last synced: about 2 months ago
JSON representation
Rust types for TypeScript
- Host: GitHub
- URL: https://github.com/zephraph/rust-types
- Owner: zephraph
- License: mit
- Created: 2024-03-19T19:10:32.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-15T23:04:02.000Z (10 months ago)
- Last Synced: 2025-01-04T07:25:37.929Z (4 months ago)
- Language: TypeScript
- Size: 5.86 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @justbe/rust-types
Rust inspired types for TypeScript.
## Examples
### Result type
```ts
import { Result } from "@justbe/rust-types";// Use `Result.ok` to construct an OK type
const ok = Result.ok("this works");// Use `Result.err` to construct an Error type
const err = Result.err(new Error("something went wrong!"));/// Safely coerce the results of a promise into a result type
const promiseResult = await Result.fromPromise(myPromise);// `isErr` or `isOk` can be used to check the kind of result
if (promiseResult.isErr()) {
// handle your error...
}// Use `unwrap` to get the value of the result. Throws if the result is an error.
const myValue = promiseResult.unwrap();
```### Option type
```ts
import { Option } from "@justbe/rust-types";// Use `Option.some` to construct a `Some` type
const some = Option.some("value");// Use `Option.none` to construct a `None` type
const none = Option.none();// Construct an option from a type that might be `null` or `undefined`
const optionalValue = Option.from(someValue);// `isSome` or `isNone` can be used to check the state of the value
if (optionalValue.isNone()) {
}
```