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

question: How to correctly use plainToInstance with Set<T>? #1446

Open
sorashi opened this issue Feb 2, 2023 · 5 comments
Open

question: How to correctly use plainToInstance with Set<T>? #1446

sorashi opened this issue Feb 2, 2023 · 5 comments
Labels
type: question Questions about the usage of the library.

Comments

@sorashi
Copy link

sorashi commented Feb 2, 2023

I was trying to use a Set, which should be supported according to readme. Minimal example:

class Person {
    @Type(() => Number)
    public numbers: Set<number> = new Set()
}

const person = new Person()
person.numbers = new Set([1,2,3,4,5])
const plain = instanceToPlain(person)
const instance = plainToInstance(Person, plain)

console.log(person)
console.log(plain)
console.log(instance)

The problem:
Actual output:

Person { numbers: Set { 1, 2, 3, 4, 5 } }
{ numbers: [ 1, 2, 3, 4, 5 ] }
Person { numbers: [ 1, 2, 3, 4, 5 ] }

Expected output:

Person { numbers: Set { 1, 2, 3, 4, 5 } }
{ numbers: [ 1, 2, 3, 4, 5 ] }
Person { numbers: Set { 1, 2, 3, 4, 5 } }

How do I use it correctly?

@sorashi sorashi added the type: question Questions about the usage of the library. label Feb 2, 2023
@sorashi
Copy link
Author

sorashi commented Feb 3, 2023

Solved using this

class Person {
    @Transform(value => new Set(value.value))
    public numbers: Set<number> = new Set()
}

But note that readme specifies this should work, which it doesn't

class-transformer/README.md

Lines 736 to 737 in 05b0151

@Type(() => Skill)
skills: Set<Skill>;

What is the problem here?

@ghost
Copy link

ghost commented Jun 6, 2023

I just ran into this issue as well. I actually checked out the master codebase and ran this test, which passed.

  it('Creates a Set', () => {
    class TestSet {
      @Type(() => Set)
      test: Set<string>;

      constructor(data?: any) {
        Object.assign(this, plainToInstance(this.constructor as any, data))
      }
    }
    const record = new TestSet({
      test: ['a']
    })
    console.log(record.test.add('b'))
  })

I then did the exact same thing in my project using the same version (0.5.1), but received an error about add not being a function. I tried to trace plainToInstance, and I saw that it was accessing the CJS libs, so I'm not sure if that has something to do with it, but it's the only difference I could spot.

Regardless, @sorashi, your fix worked for me, thank you!

@diffy0712
Copy link

Hello,

could you provide some details on your environment please? The best would be if you could create a demo environment (repo, or stackblitz or something like that), which would help to reproduce the error.

Thanks.

@sorashi
Copy link
Author

sorashi commented May 23, 2024

@diffy0712 Here you go https://github.com/sorashi/class-transformer-1446-repro

I observed this a year ago, so I tried replicating my environment back then. It's Vite+React+TypeScript app. The bug can be observed in the browser console. Instructions are in the readme in the repo. The code that uses class-transformer is in App.tsx.

@diffy0712
Copy link

Thank you, I will take a look!

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