# flow-runtime

> A flow compatible type system for JS.

Latest version **0.17.0** (published 2018-02-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install flow-runtime
pnpm add flow-runtime
yarn add flow-runtime
bun add flow-runtime
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.17.0 |
| Published | 2018-02-08 |
| First published | 2016-11-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 2.4 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 797 |
| Author | Charles Pick |
| Maintainers | codemix |

## Links

- npm: https://www.npmjs.com/package/flow-runtime
- Repository: https://github.com/codemix/flow-runtime
- Homepage: https://codemix.github.io/flow-runtime
- Issues: https://github.com/codemix/flow-runtime/issues
- npm.io page: https://npm.io/package/flow-runtime

## Recent versions

- 0.17.0 (latest) — 2018-02-08
- 0.16.0 — 2017-11-16
- 0.15.0 — 2017-11-15
- 0.14.0 — 2017-07-24
- 0.13.0 — 2017-06-25
- 0.12.0 — 2017-05-02
- 0.11.1 — 2017-04-30
- 0.11.0 — 2017-04-30
- 0.10.0 — 2017-03-16
- 0.9.1 — 2017-03-11
- 0.9.0 — 2017-03-11
- 0.8.0 — 2017-03-07
- 0.7.0 — 2017-03-04
- 0.6.1 — 2017-02-27
- 0.6.0 — 2017-02-24
- … 13 more at https://npm.io/package/flow-runtime/versions

## README

# Flow Runtime

A runtime type system for JavaScript with full [Flow](https://flowtype.org/) compatibility.


## What?

Provides a rich API for defining, inspecting and verifying data types in JavaScript. Any value that can be represented in JS can be represented by `flow-runtime`, including full support for polymorphism and parameterized types.


[See the docs for more information](https://codemix.github.io/flow-runtime/#/docs).

## Usage

```js
import t from 'flow-runtime';

const number = t.number();
const string = t.string();

string.accepts('foo'); // true
string.accepts(123); // false
number.accepts(123); // true

string.assert('Hello World!'); // ok
string.assert(false); // throws
number.assert(456); // ok
number.assert('nope'); // throws

const numberOrString = t.union(number, string);

numberOrString.assert(123); // ok
numberOrString.assert("baz"); // ok
numberOrString.assert(false); // throws

const fooOrBar = t.union(
  t.string('foo'),
  t.string('bar')
);

fooOrBar.assert('foo'); // ok
fooOrBar.assert('bar'); // ok
fooOrBar.assert('qux'); // throws

const Thing = t.object(
  t.property('name', t.string()),
  t.property('url', t.nullable(t.string()))
);

Thing.assert({
  name: 'Example',
  url: 'http://example.com/'
}); // OK


Thing.assert({
  name: 'Example'
}); // OK

Thing.assert({
  name: false
}); // throws

const arrayOfStrings = t.array(t.string());

arrayOfStrings.assert()

// ---------------------------------------------

const UserStatus = t.union(
  t.string('PENDING'),
  t.string('ACTIVE'),
  t.string('INACTIVE')
);

const PreferenceName = t.union(
  t.string('marketingOptIn'),
  t.string('darkColourScheme')
);

const UserPreferences = t.object(
  t.indexer(PreferenceName, t.boolean())
);

const User = t.object({
  id: t.number(),
  name: t.string(),
  email: t.string(),
  status: UserStatus,
  preferences: UserPreferences
});

const validUser = {
  id: 123,
  name: 'Sally',
  email: 'sally@example.com',
  status: 'PENDING',
  preferences: {
    marketingOptIn: true
  }
};

const invalidUser = {
  id: false, // invalid
  name: 'Bob',
  email: 'bob@example.com',
  status: 'NOPE', // invalid
  preferences: {
    marketingOptIn: true,
    nope: true // invalid
  }
};

User.accepts(validUser); // true
User.accepts(invalidUser); // false

User.assert(validUser); // OK
User.assert(invalidUser); // throws TypeError

```

---
_Source: https://npm.io/package/flow-runtime · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
