https://github.com/constraintautomaton/result-interface-ts
A tiny utility to standardize how to handle results that may succeed or fail inspired by Go-style error handling.
https://github.com/constraintautomaton/result-interface-ts
type-safety typescript typing
Last synced: about 1 year ago
JSON representation
A tiny utility to standardize how to handle results that may succeed or fail inspired by Go-style error handling.
- Host: GitHub
- URL: https://github.com/constraintautomaton/result-interface-ts
- Owner: constraintAutomaton
- License: mit
- Created: 2025-04-06T12:07:37.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-17T18:16:21.000Z (about 1 year ago)
- Last Synced: 2025-04-22T19:19:31.908Z (about 1 year ago)
- Topics: type-safety, typescript, typing
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/result-interface
- Size: 43.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# result-interface


A tiny utility (mainly interfaces) with zero dependencies to standardize handling results that may succeed or fail, inspired by Go-style error handling.
## Installation
```bash
npm i result-interface
```
## Usage
You can define functions more declaratively when working with possible failures:
```ts
import { type Result, isError } from "result-interface";
let VALUE: number | undefined = undefined;
function getValue(): Result {
if (VALUE !== undefined) {
return { value: VALUE };
}
return { error: "The value is undefined" };
}
const resp: Result = getValue();
if (isError(resp)) {
console.log(`Unable to get the value. Reason: ${resp.error}`);
process.exit(1);
}
console.log(`The value multiplied by two is ${resp.value * 2}`);
```
You can specify that a `Promise will not throw`:
```ts
import { isError, SafePromise, type Result } from "result-interface";
let VALUE: number | undefined = undefined;
async function getValueLater(): SafePromise {
return new Promise((resolve, reject) => {
if (VALUE !== undefined) {
resolve({
value:VALUE
})
} else {
reject("The value is undefined");
}
});
}
// Creates a promise that always resolves with a Result.
// On failure, it resolves with an error instead of rejecting or throwing.
const resp: Result = await getValueLater();
if (isError(resp)) {
console.log(`Unable to get the value. Reason: ${resp.error}`);
process.exit(1);
}
console.log(`The value multiplied by two is ${resp.value * 2}`);
```
You can specify that a promise will never fail, thus that it will always be an `IResult`:
```ts
import { SafePromise, type Result } from "./src/index";
let VALUE: number | undefined = undefined;
async function getValueLater(): SafePromise {
return new Promise((resolve, reject) => {
if (VALUE !== undefined) {
resolve({
value: VALUE
})
} else {
reject("The value is undefined");
}
});
}
// Creates a promise that always resolves with a Result.
// On failure, it resolves with an error instead of rejecting or throwing.
const resp: Result = await getValueLater();
console.log(`The value multiplied by two is ${resp.value * 2}`);
```
You can ensure that a `Promise` will not throw:
```ts
import { isError, safePromise, type Result } from "result-interface";
let VALUE: number | undefined = undefined;
async function getValueLaterUnsafe(): Promise {
return new Promise((resolve, reject) => {
if (VALUE !== undefined) {
resolve(VALUE)
} else {
reject("The value is undefined");
}
});
}
// Creates a promise that always resolves with a Result.
// On failure, it resolves with an error instead of rejecting or throwing.
const resp: Result = await safePromise(getValueLaterUnsafe());
if (isError(resp)) {
console.log(`Unable to get the value. Reason: ${resp.error}`);
process.exit(1);
}
console.log(`The value multiplied by two is ${resp.value * 2}`);
```
You can use helper functions to generate `IError` and `IResult` types (the possible types of `Result`).
```ts
import { type Result, isError, result, error } from "result-interface";
function getValue(): Result {
if (VALUE !== undefined) {
return result(Value);
}
return error("The value is undefined");
}
```
## Test
```bash
bun test
```
## License
This project is licensed under the [MIT License](./LICENSE). See the LICENSE file for more details.