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

typified

v0.7.0

Published

An experimental implementation of first class functional types using pure ES at runtime, inspired by Haskell, PureScript and Idris.

Downloads

17

Readme

An experimental implementation of first class functional types using pure ES at runtime, inspired by Haskell, PureScript and Idris.

Travis   npm   License MIT   Coveralls   code style: prettier

npm: npm install typified --save cdn: https://cdn.jsdelivr.net/npm/typified@latest/src/index.js

Getting Started

With Typified you introduce JavaScript to the world of strong typing in a functional fashion. Instead of the Java-style typing that TypeScript and Flow provide, Typified takes the Haskell-inspired approach where types are kept entirely separate from their associated values.

import ꓽ from 'typified';

const sayHello =
    t`sayHello ∷ String → String` (name => `Hello ${name}!`);

By invoking sayHello with a String we're guaranteed to be returned a String otherwise Typified will throw a TypeMismatchError that will notify the developer of such an occurrence. Passing in anything other than String will throw the mismatch error.

Typified tries to stay close to the JavaScript world, and thus allows specifying the union operator (|) to accept multiple types. For instance our sayHello function could take a Number type as well and be perfectly fine, as it's not performing any String-specific operations – thus we could augment the types to allow Number to be passed too.

const sayHello =
    t`sayHello ∷ String|Number → String` (name => `Hello ${name}!`);

Invoking sayHello with a Number type would be just as fine as invoking it with a String.

Typified also supports to the concept of generics. Below we have modified the function a little to accept two name parameters, which yields either one or two greetings depending on whether the names are the same.

const sayHello =
    t`sayHello ∷ String → String → String` ((first, second) => {
        return first === second
            ? `Hello ${first}!`
            : `Hello ${first} & ${second}!`;
    });

We've removed the Number type constraint as we always want to compare String types with each other. Invoking sayHello as sayHello('Adam', 100) would be somewhat pointless as a String and Number can never match with strict equality. However we're still performing a non-String exclusive operation, and therefore being able to pass a Number would still be rather useful.

You might be tempted to define the type as String|Number → String|Number → String however that would still allow the passing of a String and a Number at the same time. What we need is a way to enforce any type as long as both match – generics suddenly become a useful tool.

const sayHello =
    t`sayHello ∷ ∀ a. a → a → String` ((first, second) => {
        return first === second
            ? `Hello ${first}!`
            : `Hello ${first} & ${second}!`;
    });

Using generics we have the above type annotation which takes a generic a – the a doesn't yet have a concrete type, and will instead assume a concrete type when the function is invoked. Passing a Number as firstName would cause a to be of type Number, and therefore ensure that secondName is also a Number. Likewise passing String as the first parameter would give a a type of String.

You can define as many generics as you wish in the type annotation, but it's crucial that you define them using or forall as otherwise the types would be assumed to be concrete types. In Haskell it's perfectly valid to not define generics, whereas in PureScript the defining of generics is mandatory – Typified also takes this approach.