Package detail

micro-result

MBuchalik1.1kMIT1.0.3

A very simple TypeScript result type

typescript, type

readme

micro-result

A very simple TypeScript result type

When dealing with function results, it is often very convenient to have a way to express success and failure. Throwing errors is simply not type safe. This small library exports very simple types that help expressing success and failure.

Usage

Install this library using the following command:

npm install micro-result

Now, you can use the Result type:

import { Result } from 'micro-result';

const result: Result<number> = {
  success: true,
  data: 123,
};

If the Generic is (potentially) undefined, the data field may be left out:

const result: Result<number | undefined> = {
  success: true,
};

A type for the error case can be defined using the second Generic:

const result: Result<number, string> = {
  success: false,
  error: 'my-error',
};

changelog

1.0.3 (2023-04-20)

Misc

  • The package is now published using the --provenance flag.

1.0.2 (2023-04-17)

Misc

  • Updated npm dependencies.

1.0.1 (2023-01-04)

Fixed

  • Added a main JS file (index.js). Prior to this change, using import { Result } from 'micro-result' led to issues in some build environments, forcing you to use import type { Result } from 'micro-result'.

1.0.0 (2022-12-27)

This is the very first stable release.