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

use-infer

v1.0.9

Published

A lightweight TypeScript utility for automatically inferring proper JavaScript types from string values, designed for processing form data, API responses, and other string-based inputs.

Readme

use-infer

A lightweight TypeScript utility for automatically inferring proper JavaScript types from string values, designed for processing form data, API responses, and other string-based inputs.

TypeScript Vitest MIT License

Table of Contents

Installation

npm install use-infer

Core Features

  • 🎯 Automatically converts string values to their proper JavaScript types
  • 🔄 Handles nested objects and arrays
  • ⚡ Type-safe with full TypeScript support
  • 🛡️ Preserves original values when conversion isn't appropriate
  • 📦 Zero dependencies (except lodash utilities)

Usage

Basic Usage

import infer from 'use-infer';

// Simple object inference
const input = {
	isActive: 'true',
	count: '42',
	name: 'John'
};

const result = infer(input);
// Result:
// {
//   isActive: true,  // boolean
//   count: 42,       // number
//   name: 'John'     // string
// }

Advanced Usage

import infer from 'use-infer';

// Complex nested structure
const input = {
	user: {
		id: '123',
		settings: {
			notifications: 'true',
			theme: 'dark'
		}
	},
	scores: ['42', '98', '75'],
	active: 'true'
};

const result = infer(input);
// Result:
// {
//   user: {
//     id: 123,
//     settings: {
//       notifications: true,
//       theme: 'dark'
//     }
//   },
//   scores: [42, 98, 75],
//   active: true
// }

API Reference

infer

function infer<T extends OutputValue>(obj: InputValue): T;

Recursively processes an object or array, converting string values to their appropriate types.

Type Definitions

type InputPrimitive = string | number | boolean | null | undefined;
type InputObject = { [key: string]: InputValue };
type InputArray = InputValue[];
type InputValue = InputPrimitive | InputObject | InputArray;

type OutputPrimitive = string | number | boolean | null | undefined;
type OutputObject = { [key: string]: OutputValue };
type OutputArray = OutputValue[];
type OutputValue = OutputPrimitive | OutputObject | OutputArray;

inferValue

function inferValue(value: InputPrimitive): OutputPrimitive;

Processes a single value, converting it to the appropriate type if possible.

Examples

Form Data Processing

import infer from 'use-infer';

const formData = {
	username: 'john_doe',
	age: '25',
	subscribed: 'true',
	preferences: {
		emailNotifications: 'true',
		theme: 'dark',
		refreshRate: '60'
	}
};

const processedData = infer(formData);

API Response Handling

import infer from 'use-infer';

const apiResponse = {
	id: '12345',
	items: ['1', '2', '3'],
	metadata: {
		isValid: 'true',
		count: '42',
		tags: ['tag1', 'tag2']
	}
};

const typedResponse = infer(apiResponse);

Testing

The library includes comprehensive test coverage. Run tests with:

npm test

Test coverage includes:

  • Basic type inference
  • Nested object handling
  • Array processing
  • Edge cases
  • Type safety

👨‍💻 Author

Felipe Rohde

License

MIT