0.1.1 • Published 7 years ago

flow-newtype v0.1.1

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

flow-newtype

Flow implementation of NewType

Install

$ npm install flow-newtype
# or
$ yarn add flow-newtype

Usage

We can easily give new names to an existing types by extending the NewType class.

For example, we can create a Url type which simply wraps strings:

import { NewType } from 'flow-newtype'

// Define the `Url` type
class Url extends NewType<string> {}

// Create instances using the `new` operator
const googleUrl = new Url(`https://google.com/`)

// Or with `of`, which is provided as a static method
const githubUrl = Url.of(`https://github.com/`)

Now we can use the type in annotations:

// Require `Url` values instead of just a plain `string`
function fetchUrl(url: Url): Promise<Response> {

  // Unwrap the value using the `unwrap` instance method
  const urlString = url.unwrap()

  // Do stuff with `urlString`
}