Skip to content

Commit 0fe3d9e

Browse files
docs: update documentation about implicit type conversion
1 parent c623408 commit 0fe3d9e

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

README.md

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Class-transformer allows you to transform plain object to some instance of class
1111
Also it allows to serialize / deserialize object based on criteria.
1212
This tool is super useful on both frontend and backend.
1313

14-
Example how to use with angular 2 in [plunker](http://plnkr.co/edit/Mja1ZYAjVySWASMHVB9R).
14+
Example how to use with angular 2 in [plunker](http://plnkr.co/edit/Mja1ZYAjVySWASMHVB9R).
1515
Source code is available [here](https://github.com/pleerock/class-transformer-demo).
1616

1717
## What is class-transformer
@@ -103,7 +103,7 @@ Here is example how it will look like:
103103
```typescript
104104
fetch("users.json").then((users: Object[]) => {
105105
const realUsers = plainToClass(User, users);
106-
// now each user in realUsers is instance of User class
106+
// now each user in realUsers is instance of User class
107107
});
108108
```
109109

@@ -247,7 +247,7 @@ let photos = deserializeArray(Photo, photos);
247247

248248
## Enforcing type-safe instance
249249

250-
The default behaviour of the `plainToClass` method is to set *all* properties from the plain object,
250+
The default behaviour of the `plainToClass` method is to set *all* properties from the plain object,
251251
even those which are not specified in the class.
252252

253253
```typescript
@@ -274,7 +274,7 @@ console.log(plainToClass(User, fromPlainUser))
274274
// }
275275
```
276276

277-
If this behaviour does not suit your needs, you can use the `excludeExtraneousValues` option
277+
If this behaviour does not suit your needs, you can use the `excludeExtraneousValues` option
278278
in the `plainToClass` method while *exposing all your class properties* as a requirement.
279279

280280
```typescript
@@ -297,7 +297,7 @@ console.log(plainToClass(User, fromPlainUser, { excludeExtraneousValues: true })
297297
// User {
298298
// id: undefined,
299299
// firstName: 'Umed',
300-
// lastName: 'Khudoiberdiev'
300+
// lastName: 'Khudoiberdiev'
301301
// }
302302
```
303303

@@ -309,7 +309,7 @@ Since Typescript does not have good reflection abilities yet,
309309
we should implicitly specify what type of object each property contain.
310310
This is done using `@Type` decorator.
311311

312-
Lets say we have an album with photos.
312+
Lets say we have an album with photos.
313313
And we are trying to convert album plain object to class object:
314314

315315
```typescript
@@ -532,7 +532,7 @@ In this case you don't need to `@Exclude()` a whole class.
532532

533533
## Skipping private properties, or some prefixed properties
534534

535-
If you name your private properties with a prefix, lets say with `_`,
535+
If you name your private properties with a prefix, lets say with `_`,
536536
then you can exclude such properties from transformation too:
537537

538538
```typescript
@@ -563,7 +563,7 @@ export class User {
563563
get name() {
564564
return this.firstName + " " + this.lastName;
565565
}
566-
566+
567567
}
568568

569569
const user = new User();
@@ -750,8 +750,8 @@ export class Photo {
750750
}
751751
```
752752

753-
Now when you call `plainToClass` and send a plain representation of the Photo object,
754-
it will convert a date value in your photo object to moment date.
753+
Now when you call `plainToClass` and send a plain representation of the Photo object,
754+
it will convert a date value in your photo object to moment date.
755755
`@Transform` decorator also supports groups and versioning.
756756

757757
### Advanced usage
@@ -765,7 +765,7 @@ The `@Transform` decorator is given more arguments to let you configure how you
765765
| Argument | Description
766766
|--------------------|---------------------------------------------------------------------------------|
767767
| `value` | The property value before the transformation.
768-
| `obj` | The transformation source object.
768+
| `obj` | The transformation source object.
769769
| `type` | The transformation type.
770770

771771
## Other decorators
@@ -799,7 +799,7 @@ class User {
799799
}
800800

801801
class UserController {
802-
802+
803803
@TransformClassToPlain({ groups: ['user.email'] })
804804
getUser() {
805805
const user = new User();
@@ -825,6 +825,31 @@ Once TypeScript team provide us better runtime type reflection tools, generics w
825825
There are some tweaks however you can use, that maybe can solve your problem.
826826
[Checkout this example.](https://github.com/pleerock/class-transformer/tree/master/sample/sample4-generics)
827827

828+
## Implicit type conversion
829+
830+
> **NOTE** If you use class-validator together with class-transformer you propably DON'T want to enable this function.
831+
832+
Enables automatic conversion between built-in types based on type information provided by Typescript. Disabled by default.
833+
834+
```ts
835+
import { IsString } from 'class-validator'
836+
837+
class MyPayload {
838+
839+
@IsString()
840+
prop: string
841+
}
842+
843+
844+
const result1 = plainToClass(MyPayload, { prop: 1234 }, { enableImplicitConversion: true });
845+
const result2 = plainToClass(MyPayload, { prop: 1234 }, { enableImplicitConversion: false });
846+
847+
/**
848+
* result1 will be `{ prop: "1234" }` - notice how the prop value has been converted to string.
849+
* result2 will be `{ prop: 1234 }` - default behaviour
850+
*/
851+
```
852+
828853
## How does it handle circular references?
829854

830855
Circular references are ignored.
@@ -851,7 +876,7 @@ this.http
851876

852877
You can also inject a class `ClassTransformer` as a service in `providers`, and use its methods.
853878

854-
Example how to use with angular 2 in [plunker](http://plnkr.co/edit/Mja1ZYAjVySWASMHVB9R).
879+
Example how to use with angular 2 in [plunker](http://plnkr.co/edit/Mja1ZYAjVySWASMHVB9R).
855880
Source code is [here](https://github.com/pleerock/class-transformer-demo).
856881

857882
## Samples

0 commit comments

Comments
 (0)