Skip to content

kizahasi/result

Repository files navigation

@kizahasi/result

GitHub npm version minified size CI publish

Represents a value which is either OK or error.

Installation

Run npm install @kizahasi/result or yarn add @kizahasi/result

Usage

import { Result } from '@kizahasi/result';

const okObj = Result.ok(1); // Creates an "ok" object
// const okObj: Result<number> = { isError: false, value: 1 }; // …or you can create the object like this instead
if (okObj.isError) {
    console.log(okObj.error); // This cannot happen
} else {
    console.log(okObj.value); // output: 1
}

const errorObj = Result.error('ERROR!'); // Creates an "error" object
// const errorObj: Result<ANY-VALUE-TYPE> = { isError: true, error: 'ERROR!' }; // …or you can create the object like this instead
if (errorObj.isError) {
    console.log(errorObj.error); // output: ERROR!
} else {
    console.log(errorObj.value); // This cannot happen
}