npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@brigand/babel-plugin-flow-runtime

v0.17.0-brigand.2

Published

Transforms flow type annotations into flow-runtime types, optionally adds runtime type validation to annotated code.

Downloads

6

Readme

Babel Plugin Flow Runtime

A babel plugin which transforms Flow annotations into Type instances available at runtime, and optionally checks values against those types.

Supports all of flow's syntax, aims for full compatibilty with flow, found a bug? Please report it.

What?

Turns code like this:

type User = {
  id: number;
  name: string;
};

Into code like this:

import t from 'flow-runtime';
const User = t.type('User', t.object(
  t.property('id', t.number()),
  t.property('name', t.string())
));

Which you can then use like this:

User.assert({id: 123, name: 'Sally'}); // ok
User.assert({id: false, name: 'Bob'}); // throws

Installation

This plugin has a runtime dependency on flow-runtime, so make sure you install that along with this package:

npm install --save-dev babel-plugin-flow-runtime
npm install --save flow-runtime

Next, add the following to your babel configuration or .babelrc:

{
  "plugins": [["flow-runtime", {
    "assert": true,
    "annotate": true
  }]]
}

Options

The plugin supports the following options:

  • assert - Boolean, indicates whether types should be asserted at runtime. Defaults to true if process.env.NODE_ENV === 'development', otherwise false.
  • annotate - Boolean, indicates whether object or function values that have type annotations should be decorated with those types at runtime. Defaults to true.
  • libraryName - String, indicates which runtime to use. Defaults to flow-runtime

If assert is true, the following code:

const add = (a: number, b: number): number => a + b;

will be transformed into:

import t from 'flow-runtime';
const add = (a, b) => {
  let _aType = t.number();
  let _bType = t.number();
  const _returnType = t.return(t.number());
  t.param('a', _aType).assert(a);
  t.param('b', _bType).assert(b);
  return _returnType.assert(a + b);
};

Which is very safe, and can be very useful during development, but has a non-trivial performance overhead. It's usually a good idea to disable this feature in production.

If annotate is true, the following:

const add = (a: number, b: number): number => a + b;

will be transformed into:

import t from 'flow-runtime';
const add = t.annotate(
  (a, b) => a + b,
  t.function(
    t.param('a', t.number()),
    t.param('b', t.number()),
    t.return(t.number())
  )
);

Now invoking add(x, y) does not incur any overhead, as the parameters are not checked, but the type information is preserved and available for inspection:

console.log(String(t.typeOf(add))); // (a: number, b: number) => number

If both assert and annotate are false then value annotations are ignored, but type aliases are still transformed:

type User = {
  id: number;
  name: string;
};

turns into:

import t from 'flow-runtime';
const User = t.type('User', t.object(
  t.property('id', t.number()),
  t.property('name', t.string())
));

React Prop Types

When the plugin encounters a React component with a props type annotation, the annotation is converted to react prop types:

import React from 'react';

type Props = {
  name: string;
};

export class App extends React.Component<void, Props, void> {
  render () {
    return <h1>{this.props.name}</h1>;
  }
}

Becomes

import t from 'flow-runtime';
import React from 'react';

const Props = t.type('Props', t.object(
  t.property('name', t.string())
));

export class App extends React.Component {
  static propTypes = t.propTypes(Props);
  render () {
    return <h1>{this.props.name}</h1>;
  }
}