JSPM

micro-result

1.0.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 152
  • Score
    100M100P100Q82255F
  • License MIT

A very simple TypeScript result type

Package Exports

  • micro-result
  • micro-result/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (micro-result) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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',
};