JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 12
  • Score
    100M100P100Q55602F
  • License Apache-2.0

Variant, Option and Result types for Typescript

Package Exports

  • variant-ts
  • variant-ts/dist/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 (variant-ts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Variant-ts

Variant, Option and Result types for Typescript

Variants

import { Variant, variant, pattern } from "variant-ts";
import { match } form "ts-pattern";

type Connection = Variant<"IPV4", string> | Variant<"IPV6", string>;

let connection = variant<Connection>();

let con1 = connection("IPV4", "127.0.0.1");

let con2 = connection("IPV6", "2001:0db8:85a3:08d3:1319:8a2e:0370:7344");

match(con2)
    .with(pattern("IPV4"), res => {connect_ipv4(res.value)})
    .with(pattern("IPV6"), res => {connect_ipv6(res.value)})
    .exhaustive()

Options

import { none, some } from "variant-ts";

const one = some("foo");
const two = none();

const three = one.map((x) => x.concat("bar"));
const four = two.map((x) => x.concat("bar"));

expect(three.value).to.equal("foobar");
expect(four.value).to.equal(undefined);

Results

import { err, ok } from "variant-ts";

const one = ok("foo");
const two = err("error");

const three = one.map((x) => x.concat("bar"));
const four = two.map((x) => x.concat("bar"));

expect(three.value).to.equal("foobar");
expect(four.value).to.equal("error");