Getting Started
The class-transformer package is a zero-dependency utility library helping you to quickly transform class instances to plain objects and vice-versa. It works well with the class-validator library. The main features include:
conditionally transforming object properties
excluding specific properties from the transformed object
exposing properties under a different name on the transformed object
supports both NodeJS and browsers
fully three-shakable
zero external dependencies
Installation
To start using class-transformer install the required packages via NPM:
npm install class-transformer reflect-metadataImport the reflect-metadata package at the first line of your application:
import 'reflect-metadata';
// Your other imports and initialization code
// comes here after you imported the reflect-metadata package!As the last step, you need to enable emitting decorator metadata in your Typescript config. Add these two lines to your tsconfig.json file under the compilerOptions key:
"emitDecoratorMetadata": true,
"experimentalDecorators": true,Now you are ready to use class-transformer with Typescript!
Basic Usage
The most basic usage is to transform a class to a plain object:
Last updated
Was this helpful?