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

@jeroenhuinink/tsmapper

v0.0.13

Published

JSON to Typescript object mapper

Readme

Tsmapper

Library to map JSON (API responses) to typed objects for Typescript.

I like types, because when you have type information your IDE and other tools can help you avoid mistakes. Because I like types, I also like to use Typescript for frontend and backend development. A lot of what we do as either frontend and backend developers is interpret bodies sent as as part of (REST) API requests or responses. Because REST is in itself untyped and the request and response body for an HTTP request can contain arbitrary data, I like to not make any assumptions about the types of data and if data comes in an unexpected format you should fail fast to avoid hard to track errors down the line.

This library helps you interpret (map) API responses and return a guaranteed typescript (interface) type.

This library took inspiration from mappet and the yargs type definitions.

Features

  • Fields can be renamed.
  • Ensures presence of all fields. Fields can be declared optional.
  • Coerce field value to expected type.
  • Strip all fields in JSON that are not recognized.
  • Fluent interface to define the mapper.
  • Derive type definition from mapper definition. No need for a seperate interface defintion.
  • Can specify default values for missing fields. Specifing a default value null makes the field optional.
  • Can specify modifier functions to modify values or coerce field values using your own functions. The modifier function can access all fields in the input.
  • Can specify include functions to dynamically determine if field must be included, based on all fields in the input.

Installation

npm install @jeroenhuinink/tsmapper

or

yarn add @jeroenhuinink/tsmapper

Usage

Basic Usage

You can call the createMapper function to create a mapper. The function has an optional string parameter for the mapper name. This will be included in error messages. The create mapper has a mapper.map(source) function that will map a source JSON to a target as defined by the fields added to the mapper..

You can use .field(name, options) to add field declarations to the mapper. The name is the field name that is should have in the target. In the options you can specify a key for the name that the field has in the source. You can also specify default values and modifier functions. If the type can not be derived from the default value or the modifier function you can also specify a type. Otherwise a type of unknown will be drived.

import {createMapper} from '@jeroenhuinink/tsmapper';

const userMapper = createMapper('UserMapper')
  .field('id', { key: 'userId', type: 'number' })
  .field('givenName', { type: 'string' })
  .field('familyName', { type: 'string' })
  .field('email', { type: 'string' })
  .field('isAdmin', { type: 'boolean' });

const user = userMapper.map({
  userId: '12',
  givenName: 'Jeroen',
  familyName: 'Huinink',
  email: '[email protected]',
  isAdmin: false,
});

You can use type User = ReturnType<typeof userMapper.map> to get the type of the target.

More usage examples can be found in the examples subdirectory of the tests directory.