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-utilsyarn add @dbidwell94/ts-utilspnpm add @dbidwell94/ts-utilsDocumentation 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 valueimport { 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.
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch). - Make your changes.
- Commit your changes (
git commit -m 'Add some feature'). - Push to the branch (
git push origin feature-branch). - Create a new Pull Request.
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.