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

json2tsmodel

v1.0.0

Published

[![npm version](https://badge.fury.io/js/json2tsmodel.svg)](https://badge.fury.io/js/json2tsmodel) [![Travis](https://img.shields.io/travis/ddramone/json2tsmodel.svg)](https://travis-ci.org/ddramone/json2tsmodel) [![Coveralls](https://img.shields.io/cover

Readme

JSON to Typescript Data Models

npm version Travis Coveralls semantic-release

The easiest and cleanest way to cast json to data models using typescript decorators.

Sneak peek:

class Human extends BaseModel {

  @Cast()
  username: string;

  @Cast('first_name')
  name: string;

  @Cast('age', v => v>18)
  isAdult: boolean;

} 

const myself = new Human().cast({
  "username": "ddramone"
  "first_name": "Ika"
  "age": 26
});

myself.username // "ddramone"
myself.name // "Ika"
myself.isAdult // true

And there are way more...

Why to use

  • If you want to have real model classes instead of interfaces
  • If manually deserializing json data to class properties feels bad and looks ugly for you
  • If you don't want to update every part of your code if json structure changes from your api
  • If you want to have flexible way of casting json data down to data models by appling accumilator functions to them

Install

npm i json2tsmodel

Define Model

The very first step to use library is to define model by extending it from the BaseModel.

import { BaseModel } from 'json2tsmodel';

class MyModel extends BaseModel {
}

const instance = new MyModel()

Using @Cast decorator

Idea of using @Cast decorator it is to map expected json structure to model properties.

Followig example defines id property that conforms uuid from JSON.

class User extends BaseModel {
 @Cast('uuid') id: string;
}

new u = new User().cast(
    { uuid: 'u_1' }
)

Using @Cast decorator is flexible. There are multiple ways to use it.

  • @Cast('uuid') id: string; With alias string as an only argument when you want your class property to conform json structure
  • @Cast() id: string; Without any arguments when alias of the property name matches json structure.
  • @Cast(v => +v) age: number; With transformer function as an only argument when you want to apply function to value while casting data to model instance. example above ensures that the type of the age is always number Transformer function takes 2 arguments:
    • value that is being casted based on defined alias.
    • raw whole data that is being casted.
  • @Cast('age', v => v>=18) isAdult: boolean; Combination of both alias and transformer function

Using @HasMany and @HasOne decorators

Id your model contains properties that are seperate models themselves, than using @HasMany and @HasOne will make your life easier when you want to cast whole object in one go.

Both of docerators take sub-model class reference as an argument.

example:

class Location extends BaseModel {
    @Cast()
    country: string;
    
    @Cast()
    city: string;
}

class User extends BaseModel {
    @Cast()
    name: string;
    
    @HasOne(Location)
    @Cast() 
    address: Location;
}

const user = new User();
user.cast({
    name: 'Ika',
    address: {
        city: 'Batumi',
        country: 'Georgia',
    }
})

Obviously type of user.address will be Location and it will contain both country and city values.

@HasMany decorator does the same, but for arrays

Example:

class Book {
    @Cast() isbn: string;
}

class Author extends BaseModel {
    
    @Cast() name: string;
    
    @HasMany(Book)
    @Cast() books: Book[];
 }
 
 const author = new Author().cast({
    name: 'Philip K. Dick'
    books: [
        { isbn: '9780345404473' }
        { isbn: '9780679740667' }
    ]
 })

... have fun 🖖