Type manager is a parsing package for TypeScript which will help you to transform JSON strings or plain objects into JavaScript object instances. It s

GitHub - dipscope/TypeManager.TS: A package which will help you to transform your JSON strings or plain objects into JS object instances.

submited by
Style Pass
2022-05-21 20:00:19

Type manager is a parsing package for TypeScript which will help you to transform JSON strings or plain objects into JavaScript object instances. It supports decorators or declarative configuration and allows you to configure parsing of your or 3rd party classes easily.

We recommend to use our official website to navigate through available features. You can also use the latest documentation described below.

Sometimes we want to transform plain objects to the classes we have. Let's assume we are loading a users JSON data from our backend API or the other datasource:

JSON.parse function returns plain objects and we actually lied to compiler when said that it is a User class array. In this code we can successfully use user.id or user.name. However we cannot use user.isDeleted() because user is not an instance of a User class.

Solution is to create new instances of User class from plain objects returned by JSON.parse function. But things may go wrong once you have a more complex object hierarchy. Besides deletedAt property is represented as String in JSON but User class declares it as a Date so we also have to perform appropriate conversion when assigning this property.

Leave a Comment