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 🙏

© 2025 – Pkg Stats / Ryan Hefner

ts-runtime-picker

v2.0.2

Published

A package to dynamically create pickers based on TypeScript interfaces.

Downloads

458

Readme

ts-runtime-picker

GitHub License NPM Version NPM Downloads GitHub Actions Workflow Status GitHub Release

ts-runtime-picker 🚀 is a TypeScript-first utility package designed to dynamically transform your code and provide runtime-safe "pickers" for your objects based on TypeScript interfaces or types. The package integrates seamlessly into your Vite-based or Webpack-based projects, allowing developers to enjoy type-safe runtime logic without sacrificing development speed or flexibility.


🛠️ Problem and Solution

🐛 The Problem

When working with JavaScript or TypeScript, developers often pass objects directly into functions, APIs, or databases (like Firebase). This can lead to unnecessary or unwanted properties being included. For example:

const request = {
    data: {
        firstName: "John",
        lastName: "Doe",
        email: "[email protected]",
        password: "secret",
        extraField: "notNeeded",
        anotherExtraField: "stillNotNeeded"
    }
};

firebase.collection("users").add(request.data);

In this example, only firstName, lastName, email, and password might be relevant for the operation, but extraField and anotherExtraField are also sent, which could cause inefficiencies, validation errors, or unexpected behavior.

Even if you explicitly type request.data as User in TypeScript, the extra fields (extraField and anotherExtraField) still exist at runtime. TypeScript enforces types only at compile time, meaning that any additional or unwanted properties are not automatically removed:

interface User {
    firstName: string;
    lastName: string;
    email: string;
    password: string;
}

const request: { data: User } = {
    data: {
        firstName: "John",
        lastName: "Doe",
        email: "[email protected]",
        password: "secret",
        extraField: "notNeeded", // This still exists at runtime
        anotherExtraField: "stillNotNeeded" // This too
    }
};

firebase.collection("users").add(request.data); // `extraField` and `anotherExtraField` are still sent!

Manually filtering the object to ensure it adheres to a defined interface is tedious and error-prone:

const filteredData = {
    firstName: request.data.firstName,
    lastName: request.data.lastName,
    email: request.data.email,
    password: request.data.password
};

firebase.collection("users").add(filteredData);

💡 The Solution

ts-runtime-picker 🧰 solves this by automatically generating a picker function based on your TypeScript interface. This function ensures that only the properties defined in the interface are included in the object:

import { createPicker } from "ts-runtime-picker";

interface User {
    firstName: string;
    lastName: string;
    email: string;
    password: string;
}

const picker = createPicker<User>();
const filteredData = picker(request.data);

firebase.collection("users").add(filteredData);

The picker function dynamically removes unwanted properties, ensuring only the keys specified in User are included. This approach:

  • ⏳ Saves time by eliminating repetitive manual filtering.
  • ✅ Ensures runtime safety by aligning object properties with TypeScript interfaces.
  • 🐞 Reduces the risk of bugs and inefficiencies caused by sending unnecessary data.

📦 Installation

To start using ts-runtime-picker, follow these steps:

1. Install the Package

npm install ts-runtime-picker

2. Add the Vite Plugin or Webpack Loader

Vite Plugin

In your vite.config.ts, import the plugin and include it in the plugins array:

import { defineConfig } from "vite";
import TsRuntimePickerVitePlugin from "ts-runtime-picker/vite-plugin";

export default defineConfig({
    plugins: [TsRuntimePickerVitePlugin()],
});

Webpack Loader

For projects using Webpack, you can integrate ts-runtime-picker with the following webpack loader.

module.exports = {
    //...
    module: {
        rules: [
            {
                test: /\.ts$/,
                use: [
                    {
                        loader: 'ts-loader',
                    },
                    {
                        loader: 'ts-runtime-picker/webpack-loader', // add the ts-runtime-picker webpack loader
                    },
                ],
                include: path.resolve(__dirname, 'src'),
                exclude: /node_modules/,
            },
        ],
    },
    resolve: {
        extensions: ['.ts', '.js'],
        fallback: {
            fs: false,
            path: false,
            os: false,
            perf_hooks: false,
        }
    },
    //...
}

✨ Usage

1. Define Your TypeScript Interface

Create a TypeScript interface that defines the structure of the object you want to pick keys from:

interface User {
    firstName: string;
    lastName: string;
    email: string;
    password: string;
}

2. Use createPicker

Call the createPicker function with your TypeScript interface:

import { createPicker } from "ts-runtime-picker";

const picker = createPicker<User>();

const inputObject = {
    firstName: "John",
    lastName: "Doe",
    email: "[email protected]",
    password: "secret",
    extraField: "notNeeded",
};

const result = picker(inputObject);
console.log(result); // { firstName: "John", lastName: "Doe", email: "[email protected]", password: "secret" }

3. How It Works

The plugin dynamically transforms the createPicker<User>() call into a runtime-safe implementation that picks only the keys defined in User. This transformation works with both Vite (via the plugin) and Webpack (via the loader).


🎯 Purpose and Benefits

The goal of ts-runtime-picker is to bridge the gap between TypeScript's compile-time type safety and runtime JavaScript functionality. By transforming your code at build time, this package enables developers to:

  • 🚫 Avoid repetitive manual key picking from objects.
  • ⚡ Ensure runtime behavior aligns with TypeScript-defined interfaces.
  • 🎉 Simplify code while maintaining type safety.
  • 🛠 Works seamlessly with modern bundlers, including Vite (via a plugin) and Webpack (via a loader).

Contributing

We welcome contributions! If you'd like to improve ts-runtime-picker, feel free to open an issue or submit a pull request.

Author

License

MIT

🌟 Acknowledgements

Special thanks to the open-source community and early adopters of ts-runtime-picker for their feedback, which helped expand support to Webpack alongside Vite.