Skip to content

docs: add example of array with different types #1455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,62 @@ export class Photo {
}
```

### Array with multiple types[⬆](#table-of-contents)

You can also use array with multiple types, using the `@Transform()` decorator.

```typescript
class Photo {
type: string;
filename: string;
}

class Landscape extends Photo {
local: string;
}

class Portrait extends Photo {
person: string;
gender: string;
}

export class Album {
@Transform(({ value }) => {
return value.map(item => {
const options = {
portrait: Portrait,
landscape: Landscape,
};
const obj = options[item.type];
return plainToInstance(obj, item);
});
})
photos: Array<Landscape | Portrait>;
}
```

Is very util to add type for object arrays with dynamic types.

```typescript
const albumJson = {
photos: [
{
type: 'landscape',
filename: 'cataratas.png',
local: 'Cataratas do Iguaçu - PR/BR',
},
{
type: 'portrait',
filename: 'topmodel.jpg',
person: 'Alessandra Ambrósio',
gender: 'female',
},
],
};

const albumObject = plainToInstance(Album, albumJson);
```

Library will handle proper transformation automatically.

ES6 collections `Set` and `Map` also require the `@Type` decorator:
Expand Down
43 changes: 43 additions & 0 deletions sample/sample6-multiple-type-array/Album.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Transform, plainToInstance } from '../../src';

class Photo {
type: string;
filename: string;
}

class Portrait extends Photo {
person: string;
gender: string;
}

class Sports extends Photo {
sport: string;
origin: string;
}

export class Landscape extends Photo {
local: string;

getLocation(): string {
return `The location of this picture is ${this.local.split('-')[1].trim()}`;
}
}

export class Album {
@Transform(({ value }) => {
return value.map(item => {
const options = {
portrait: Portrait,
sports: Sports,
landscape: Landscape,
};
const obj = options[item.type];
return plainToInstance(obj, item);
});
})
photos: Array<Landscape | Portrait | Sports>;

findByType<T>(type: string): T {
return this.photos.find(item => item.type === type) as T;
}
}
30 changes: 30 additions & 0 deletions sample/sample6-multiple-type-array/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { plainToInstance } from '../../src';
import { Album, Landscape } from './Album';

const albumJson = {
photos: [
{
type: 'landscape',
filename: 'cataratas.png',
local: 'Cataratas do Iguaçu - PR/BR',
},
{
type: 'portrait',
filename: 'topmodel.jpg',
person: 'Alessandra Ambrósio',
gender: 'female',
},
{
type: 'sports',
filename: 'ferrari.png',
sport: 'F1',
origin: 'The United Kingdom',
},
],
};

const albumObject = plainToInstance(Album, albumJson);
console.log(albumObject);

const landscape = albumObject.findByType<Landscape>('landscape');
console.log(landscape.getLocation());