Last active
November 26, 2019 11:20
-
-
Save artalar/6c8f0d9d2871a433355432afef6b1292 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| 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