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

ttyped

v0.1.1

Published

Runtime type checking for JavaScript/CoffeeScript/LiveScript

Downloads

5

Readme

ttyped

Build Status

A runtime type checking library for LiveScript, JavaScript, and CoffeeScript. It has two modes, closure-based and code gen, and it uses type-check for the type syntax. It is also compatible with the ES6 decorators proposal.

It was mostly designed with LiveScript in mind, but it is very useful in any compile-to-JS dynamic language, or even JavaScript itself.

Installation

npm install --save ttyped

Node.js uses the code generation-based version by default, while the browser version uses a CSP-safe closure-based version by default. If you want to specifically use the closure-based version in Node.js, use ttyped/csp. If you want to specifically use the code-generation version in the browser through Browserify, Webpack, etc., use ttyped/gen.

Example usage

// JavaScript
import {type, Type} from "ttyped"

type.add("Greeter", "*", x => x instanceof Greeter)

class Greeter extends Type {
    @type("String")
    init(greeting) {
        this.greeting = greeting
    }

    greet() {
        return "Hello, " + this.greeting
    }

    @type("HTMLButtonElement")
    attachTo(button) {
        button.textContent = "Say Hello"
        button.onclick = () => {
            alert(type.as(doSomething(this), "Greeter").greet())
        }
    }
}

const greeter = new Greeter("world")

// Throws!
// const failedGreeter = new Greeter(Symbol("wut?"))

const button = document.createElement("button")

greeter.attachTo(button)

document.body.appendChild(button)
# CoffeeScript
{type, Type} = require 'ttyped'

type.add 'Greeter', '*', (x) -> x instanceof Greeter

class Greeter extends Type
    init: type('String') (@greeting) ->

    greet: -> "Hello, #{@greeting}"

    attachTo: type('HTMLButtonElement') (button) ->
        button.textContent = 'Say Hello'
        button.onclick = ->
            alert type.as(doSomething(this), 'Greeter').greet()

greeter = new Greeter('world')

# Throws!
# var failedGreeter = new Greeter Symbol('wut?')

button = document.createElement('button')

greeter.attachTo(button)

document.body.appendChild(button)
# LiveScript
require! ttyped: {type: t}

t.add 'Greeter', '*', (instanceof Greeter)

class Greeter extends Type
    init: type 'String' <| (@greeting) !->

    greet: -> "Hello, #{@greeting}"

    attachTo: type 'HTMLButtonElement' <| (button) !->
        button.textContent = 'Say Hello'
        button.onclick = alert . (.greet!) . ->
            (doSomething @) `t.as` 'Greeter'

greeter = new Greeter 'world'

# Throws!
# var failedGreeter = new Greeter Symbol 'wut?'

button = document.createElement 'button'

greeter.attachTo button

document.body.appendChild button

API

Type declarations

type = ttyped.type

Get a type namespace. Note that this is called by name, so two accesses do not return identical objects, even though they carry identical structure.

// JavaScript
// Function wrapper
func = type(...types)((...args) => {
    // body
})

// Decorator
class C {
    @type(...types)
    method(...args) {
        // body
    }
}
# CoffeeScript
# Function wrapper
func = type(types...) (args...) ->
    # body

# Decorator
class C
    method: type(types...) (args...) ->
        # body
# LiveScript
# Function wrapper
func = type ...types <| (...args) ->
    # body

# Decorator
class C
    method: type ...types <| (...args) ->
        # body

Create a type assertion and attach it to the function or method. It works as both a function wrapper and decorator. Note that it does not work with ES6 class constructors as values.

type.add(type, existing, validate)

Add a new type to the namespace, based on an existing type and a validate function to check it.

type.as(value, type)

Get value, simultaneously asserting that it is of type type. This is useful as both a void function and as an inline asssertion.

This is great infix in LiveScript, like so:

formatString value `type.as` 'String'
type.is(value, type)

Test if value is of type type, returning a boolean.

Also, the type function also has all the properties accessible from the module, and they point to the global equivalent.

// Create a fresh new type namespace
ttyped.type
type.type

// Equivalent calls
ttyped.check(true)
type.check(true)

// These extend the exact same class
class extends ttyped.Type {}
class extends type.Type {}

Enable/disable type checking

type.check(boolean)

Call with true to enable runtime checking, and false to disable it.

Superclass for better type checking

class ttyped.Type {}

An abstract class, mainly intended for ES6 classes, CoffeeScript and its descendants, and similar, for patterns like below:

# CoffeeScript example
{type, Type} = require 'ttyped'

class Class extends Type
    init: type("String", "Number") (name, count) ->
        @name = format(name)
        @counter = new Counter(count)
// Same in ES6
import {type, Type} from 'ttyped'

class Class extends Type {
    @type("String", "Number")
    init(name, count) {
        this.name = format(name)
        this.counter = new Counter(count)
    }
}
# Same in LiveScript
require! ttyped: {type, Type}

class Class extends Type {
    init: type "String", "Number" <| (name, count) ->
        @name = format name
        @counter = new Counter count

Issues

Use the issue tracker. Pull requests are welcome, just make sure ESLint is happy and it stays well tested.

License

ISC