What is @nestjs/mapped-types?
The @nestjs/mapped-types package is a utility module for NestJS, which provides functions to transform and map types. It is commonly used to create DTOs (Data Transfer Objects) by inheriting validation rules without the need to redefine them, thus reducing redundancy and potential for errors.
What are @nestjs/mapped-types's main functionalities?
PartialType
Creates a type (class) with all the properties of the input type set to optional. Useful for update operations where only a subset of properties may be provided.
{"@nestjs/swagger": "^5.0.0", "@nestjs/mapped-types": "^0.4.0"}
PickType
Creates a type by picking a set of properties from an existing type. This is useful when you want to expose only certain fields of a model for a particular use case.
{"@nestjs/swagger": "^5.0.0", "@nestjs/mapped-types": "^0.4.0"}
OmitType
Creates a type by omitting a set of properties from an existing type. This can be used to exclude sensitive information or irrelevant properties from being exposed.
{"@nestjs/swagger": "^5.0.0", "@nestjs/mapped-types": "^0.4.0"}
IntersectionType
Combines two types into one new type (class) that includes all properties from both types. This is useful for combining properties from multiple sources or adding additional properties to an existing type.
{"@nestjs/swagger": "^5.0.0", "@nestjs/mapped-types": "^0.4.0"}
Other packages similar to @nestjs/mapped-types
class-transformer
This package is similar to @nestjs/mapped-types in that it allows the transformation of objects to classes and vice versa. It also provides decorators to control the transformation behavior. However, it does not focus on creating mapped types but rather on the transformation process itself.
class-validator
While not directly similar to @nestjs/mapped-types, class-validator works well in conjunction with it by providing decorators to add validation rules to class properties. It is often used together with class-transformer to validate and transform data in NestJS applications.
type-fest
Type-fest is a TypeScript utility library that provides a collection of essential types for everyday use. It includes types similar to those in @nestjs/mapped-types, such as PartialDeep, RequiredDeep, etc. However, it is not specifically tailored to NestJS and does not integrate with the framework's features like @nestjs/mapped-types does.
A progressive Node.js framework for building efficient and scalable server-side applications.
Description
Mapped Types module for Nest used by the @nestjs/graphql
and @nestjs/swagger
packages.
Installation
$ npm i --save @nestjs/mapped-types
Quick Start
As you build out features, it's often useful to construct variants on a base entity type. A good example of such a variant is a Data Transfer Object (DTO). A Data Transfer Object is an object that is used to encapsulate data, and send it from one part of your application to another. DTO’s help us define the input and output interfaces of our system.
Let's imagine a real-world example, where we typically need to build both a create and update variations for the same entity type.
The create variant may require all fields, while the update variant may make all fields optional. Not to mention, both these types can also be variants of an entity type (to some extent).
That's a lot of redundant code!
Thus, NestJS now provides several utility functions that perform type transformations to help us avoid doing this, and make life a little bit easier.
Available mapped types:
PartialType
- returns a type (class) with all the properties of the input type set to optional (requirement: at least 1 validation decorator applied to each property)PickType
- constructs a new type (class) by picking a set of properties from an input typeOmitType
- constructs a type by picking all properties from an input type and then removing a particular set of keysIntersectionType
- combines two types into one new type (class)
Read more in this article.
Support
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
Stay in touch
License
Nest is MIT licensed.