Class-transformer: feat: transform class including dictionary type properties

Created on 18 Nov 2016  路  5Comments  路  Source: typestack/class-transformer

Transforming Class which have a nested Class or Class[] type properties is working well.
Great Job!

But could I also transform Class with dictionary type properties?

import {Type, classToPlain} from "class-transformer";

export interface PhotoList {
    [id: string]: Photo
}

export class Album {
    id: number;
    name: string;
    @Type(() => Photo, {dictionaryType: true})  // telling it it's a dictionaryType like this...
    photos:PhotoList = {};
}

export class Photo {
    id: number;
    filename: string;
}

let plainAlbum = classToPlain(myAlbum);
let restoredAlbum = plainToClass(plainAlbum)
needs discussion feature

All 5 comments

Yeah, this would be quite nice.
But I don't even see where you could put a decorator to make it work.
I'm pretty new to TS though.

@pplink the dictionaryType params seems not working. Is there any settings?

Hi guys!

I have removed the +1, as they don't add to the conversation, please add a thumb up emoji to the original question if you want to show your support. Thanks!

@pplink-taemin I know what you mean, but after transpile, the interface is empty, so how do you wanna tell which property is the foreign key?

I know what you mean, but after transpile, the interface is empty, so how do you wanna tell which property is the foreign key?

The received object will have it like this:

{
  'photo': Photo, 
  'car': Car,
}

So it's doable propably, however I dont understand the clase use-case for this, as you dont want to specifiy the type for every id you recieve.

~@taemini doesn't the polymorphism covers your use case? You can take a look here: #125~
Update: after re-reading this it does not solves this use-case.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Kalhama picture Kalhama  路  5Comments

SC30SC picture SC30SC  路  4Comments

sebastian-zarzycki-es picture sebastian-zarzycki-es  路  6Comments

patricknazar picture patricknazar  路  4Comments

AckerApple picture AckerApple  路  5Comments