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

@dollarshaveclub/runtype

v1.1.0

Published

Runtype converts Typescript type aliases, interfaces, and enums to Javascript that can be used during runtime

Downloads

11

Readme



Runtype converts Typescript type aliases, interfaces, and enums to Javascript that can be used during runtime

Features

  • Uses Typescript Compiler
  • Recursive validation during runtime
  • Well Tested
  • Many supported types

Installing

npm i -g @dollarshaveclub/runtype # Install globally or --save-dev

Usage

First things first, you need to have some typescript that you'd like to transpile to javascript.

Command Line API

# Input: STDIN, Output: STDOUT
$ echo 'type ID = string | number' | runtype >> ./output.js

# Input: Disk, Output: STDOUT
$ runtype -f './files/**/*.ts' >> ./output.js

# Input: Disk, Output: Disk
$ runtype -f './files/**/*.ts' -o ./output.js

# Debug
echo 'type ID = string | number' | runtype -d

Node API

import { parse, render } from '@dollarshaveclub/runtype'
import fs from 'fs'

const data = parse(['./files/my-types.ts'])
console.log(data.aliases.ID)

fs.writeFileSync(render(data), './output.js')

Runtime API

Once you've transpiled your typescript, import it in your project to be compiled into your apps build.

The transpiled API allows you to validate your data with the types and interfaces defined in your typescript files. They are functions that will throw errors if the data provided is invalid.

import {
  aliases: { ID },
  interfaces: { Product },
}  from './output.js'

ID(123)
ID('123')
ID(true) // Throws an error

Product({ sku: 'M-EXEC-1', price: 5.00 }) // etc

Additional APIs are available to work with.

import {
  runtypes, // All of your types/interfaces organized neatly
  validate, // A function that validates data, returns true or error messages
  resolveType, // A function that converts a value into a type
  aliases, // An object containing all of your type aliases
  interfaces, // An object containing all of your interfaces
  enums, // An object containing all of your enums
} from './output.js'

console.log(runtypes) // neat

validate('ID', 5) // true
validate('ID', ['test']) // ['ID value is invalid']

resolveType(5) // "number"
resolveType([]) // "array", etc

aliases.ID(true) // throws an error
interfaces.Product({ /* etc */ }})

interfaces.CartAddEvent({
  cart: enums.Carts.Gift, // Specify the gift cart as an enum
})

Support

The following features are supported by Runtype. Contributions are always welcome!

Aliases

type ID = number

Union Types

type mixed = string | number | boolean | object | symbol | null | undefined

Interfaces

interface Product {
  id: string | number, // Union Types
  sku: string,
  price: number,
  type: 'product', // Literal Values
  description?: string, // Optional Properties
  parent: Product, // Reference Types
  childProducts: Product[], // Reference Array Types
  benefits: string[] // Primitive Array Types
}

Enums

enum PaymentMethods {
  Credits = 'credits',
  Card = 'card',
  PayPal = 'paypal',
}

License

MIT