Skip to content
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

Is discriminator in @Type working for collections? #1441

Open
CaunicAndreiAteliere opened this issue Jan 26, 2023 · 1 comment
Open

Is discriminator in @Type working for collections? #1441

CaunicAndreiAteliere opened this issue Jan 26, 2023 · 1 comment
Labels
type: question Questions about the usage of the library.

Comments

@CaunicAndreiAteliere
Copy link

CaunicAndreiAteliere commented Jan 26, 2023

I was trying to use the discriminator in @type as per documentation:

  @Type(() => Photo, {
    discriminator: {
      property: '__type',
      subTypes: [
        { value: Landscape, name: 'landscape' },
        { value: Portrait, name: 'portrait' },
        { value: UnderWater, name: 'underwater' },
      ],
    },
  })
  topPhoto: Landscape | Portrait | UnderWater;

But my use case involves to use it in a Map construct like this:

  @Type(() => Photo, {
    discriminator: {
      property: '__type',
      subTypes: [
        { value: Landscape, name: 'landscape' },
        { value: Portrait, name: 'portrait' },
        { value: UnderWater, name: 'underwater' },
      ],
    },
  })
  topPhoto: Map<string, Landscape | Portrait | UnderWater>;               <-------my scenario

When testing I found out that the discriminator doesn't work anymore and I cannot find out an workaround...

Can you please help me understand how to use it in order to have sub-types based on a property in a Map construct and not the base class?

@CaunicAndreiAteliere CaunicAndreiAteliere added the type: question Questions about the usage of the library. label Jan 26, 2023
@mrbrunelli
Copy link

mrbrunelli commented Feb 16, 2023

@CaunicAndreiAteliere Did you try with Transform?

const myMap = new Map<string, Landscape | Portrait>();
myMap.set('landscape', new Landscape());
myMap.set('portrait', new Portrait());

export class CreateMyResource {

  @Transform(({ value }) => {
    const obj = myMap.get(value.type);
    return plainToInstance(obj, value)
  })
  topPhoto: Map<string, Landscape | Portrait>;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Questions about the usage of the library.
Development

No branches or pull requests

2 participants