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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ts-mapper-util

v0.0.3

Published

Type-safe bidirectional mappers between API responses, form state, and API requests

Readme

ts-mapper-util

Type-safe bidirectional mappers between API responses, form state, and API requests.

createMapper enforces exact object shapes at both definition and call sites, catching excess properties, missing fields, and type mismatches at compile time.

Installation

npm install ts-mapper-util

Usage

import { createMapper } from "ts-mapper-util";

type UserResponse = { id: number; first_name: string; last_name: string };
type UserRequest = { fullName: string };
type UserForm = { name: string };

const userMapper = createMapper<UserResponse, UserRequest, UserForm>()({
  toForm: (data) => ({
    name: `${data.first_name} ${data.last_name}`,
  }),
  toRequest: (data) => ({
    fullName: data.name,
  }),
});

const form = userMapper.toForm({ id: 1, first_name: "John", last_name: "Doe" });
// { name: "John Doe" }

const request = userMapper.toRequest({ name: "John Doe" });
// { fullName: "John Doe" }

Inferred Form type

When the Form shape can be inferred from toForm, you can omit the third generic:

const userMapper = createMapper<UserResponse, UserRequest>()({
  toForm: (data) => ({
    name: `${data.first_name} ${data.last_name}`,
  }),
  toRequest: (data) => ({
    fullName: data.name,
  }),
});

Spread operator protection

TypeScript normally skips excess property checks when objects are spread into arguments or return values. ts-mapper-util catches these cases too — a key differentiator from plain type annotations:

const extra = { fullName: "John", debug: true };

// ✅ Caught — excess property 'debug' via spread at call site
userMapper.toRequest({ ...extra });

// ✅ Caught — excess property in spread return from mapper definition
createMapper<UserResponse, UserRequest, UserForm>()({
  toForm: (data) => ({
    name: data.first_name,
    ...{ leftover: true }, // error: excess property
  }),
  toRequest: (data) => ({
    fullName: data.name,
  }),
});

This is achieved via the ExactObject utility type, which maps any key not present in the expected shape to never.

API

createMapper<Response, Request, Form>()

Curried factory that returns a mapper builder. Pass toForm and toRequest callbacks to define the mapping.

Returns a StrictMapper<Response, Form, Request> with:

  • toForm(data: Response): Form — maps an API response to form state
  • toRequest(data: Form): Request — maps form state to an API request

ExactObject<T, Shape>

Utility type that marks any key in T not present in Shape as never, preventing excess properties.

Development

npm install
npx tsc --noEmit

Type tests live in src/types.test.ts and use @ts-expect-error assertions — if tsc compiles without errors, all tests pass.