A Typescript library that provides utility types inspired by Rust's powerful type
system, including Result<T, E>
and Option<T>
. These types help to enhance
type safety and improve error handling in Typescript applications.
You can install the library via npm
:
npm i @dbidwell94/ts-utils
yarn add @dbidwell94/ts-utils
pnpm add @dbidwell94/ts-utils
Documentation is provided via typedoc
and is published on GitHub Pages
import { option, Option } from "@dbidwell94/ts-utils";
const myOption = option.some(123);
function doSomethingWithOptions(optionParam: Option<number>) {
if (optionParam.isSome()) {
console.log(`Option value is ${optionParam.value}`);
} else {
console.log("The provided Option has no value");
}
}
doSomethingWithOptions(myOption); // Option value is 123
doSomethingWithOptions(option.none()); // The provided Option has no value
function doSomethingNullishValue(val: number | null | undefined) {
doSomethingWithOptions(option.unknown(val));
}
doSomethingNullishValue(null); // The provided Option has no value
import { result, Result } from "@dbidwell94/ts-utils";
const myResult = result.ok(123);
function doSomethingWithResult(resultParam: Result<number>) {
if (resultParam.isOk()) {
console.log(`The provided value was ${resultParam.value}`);
} else {
console.log(
`The provided Result errored with ${resultParam.error.toString()}`,
);
}
}
Contributions are welcome! Please open an issue or submit a pull request if you'd like to add features or fix bugs.
git checkout -b feature-branch
).git commit -m 'Add some feature'
).git push origin feature-branch
).This project is licensed under the MIT License. See the LICENSE file for details.
Inspired by the elegant type system of Rust, this library aims to bring similar concepts to Typescript for improved type safety and error handling.