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

rutypi

v0.1.6

Published

A Webpack plugin for TypeScript to provide type data at runtime

Downloads

10

Readme

Rutypi a runtime type information and verify library Build Status Npm package version GitHub issues

Rutypi is a library which allows you to automatically export type information for runtime usage.
Additionally, it provides a mechanism to check if an object matches it's declared type out of the box.

Requirements

In order to use rutypi you will need

  1. Webpack version 5
  2. Typescript version 4

Setup

The setup is done by adding RutypiWebpackPlugin to your webpack plugins.
That's it, you're ready to start!

Minimal Webpack example (webpack.config.js):

const { RutypiWebpackPlugin } = require("rutypi/webpack");

module.exports = {
    entry: './src/index.ts',
    plugins: [
        new RutypiWebpackPlugin(),
    ],
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                exclude: /node_modules/,
                loader: "ts-loader"
            },
        ],
    },
    resolve: {
        extensions: ['.tsx', '.ts', '.js'],
    },
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist'),
    },
};

Usage

The following methods are offered by the runtime library:

typeInfo

Retrieve type information at runtime about the given type. Example:

import { typeInfo, lookupReference } from "rutypi";

type MyType = {
    key: string,
    value: number,
    ref: MyType | undefined
};

/* Value: { type: 'object-reference', target: 'T43_MyType' } */
let info = typeInfo<MyType>();
if(info.type === "object-reference") {
    /*
     * Will print:
     *  {
     *    type: 'object',
     *    members: {
     *      key: { type: 'string' },
     *      value: { type: 'number' },
     *      ref: {
     *        type: 'union',
     *        types: [
     *          { type: 'object-reference', target: 'T43_MyType' },
     *          { type: 'undefined' },
     *          { type: 'null' },
     *        ]
     *      }
     *    },
     *  }
     */
    console.log("Reference output: %o", lookupReference(info));
}

validateType

Validate the given object according to the type specification of the given template argument.
If the object doesn't match the type declaration an error will be thrown or returned. Example:

import { validateType } from "rutypi";

type MyType = {
    key: string,
    value: number,
    ref?: MyType
};

let validatedObject = validateType<MyType>({ key: "a", value: 123 });

lookupReference

Lookup type references to other types.
A type reference in this manner will most likely be a named type or interface.
An example could be found here.

Examples

Fully functionally examples can be found within the examples folder.

Limitations

This plugin can not be used with transpileOnly setting enabled for ts-loader. When enabled, the TypeScript compiler will not gather any type information nor index any .d.ts files.