Skip to content

Instantly share code, notes, and snippets.

@artalar
Last active November 26, 2019 11:20
Show Gist options
  • Select an option

  • Save artalar/6c8f0d9d2871a433355432afef6b1292 to your computer and use it in GitHub Desktop.

Select an option

Save artalar/6c8f0d9d2871a433355432afef6b1292 to your computer and use it in GitHub Desktop.
//
import * as t from "types";
// STATIC: any
// RUNTIME: string
const someInput /*: any*/ = "input";
// runtime contract for value
const SomeUnion = t.OR(t.String, t.Number);
// STATIC: string | number
// RUNTIME: string
const someValue = SomeUnion.ensure(someInput);
// STATIC: { type: string | number, validated: false, value: any }
// RUNTIME: { type: ['string', 'number'], validated: false, value: 'input' }
const someValueBox = SomeUnion.box(someInput);
// runtime contract for function
const SomeFn = t.fn([t.String], t.Void);
// STATIC: (a: string | Box<string>) => void
const someFnBox = SomeFn.box(str => console.log(str));
// STATIC: error
// RUNTIME: ok
someFnBox(someValue);
// log: "input"
// STATIC: error
// RUNTIME: ok
someFnBox(someValueBox);
// log: "input"
// RUNTIME: { type: ['string'], validated: true, value: 'input' }
someValueBox;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment