typestack / class-transformer

Decorator-based transformation, serialization, and deserialization between objects and classes.
MIT License
6.81k stars 500 forks source link

fix: No Capability to BigInt? #1702

Closed Slarper closed 4 months ago

Slarper commented 4 months ago

Description

When plainToInstance converted a plain object contains bigint fields, it crashed and output:

 Cannot mix BigInt and other types, use explicit conversions

Minimal code-snippet showcasing the problem

import 'reflect-metadata';
import { plainToInstance } from 'class-transformer';
class ExampleClass {
  // Define a bigint field
  public largeNumber: bigint;

  constructor(initialValue: bigint) {
    this.largeNumber = initialValue;
  }

  // A method to add a value to the bigint field
  public addToLargeNumber(value: bigint): void {
    this.largeNumber += value;
  }

  // A method to retrieve the bigint field
  public getLargeNumber(): bigint {
    return this.largeNumber;
  }
}
const publicKey2 = plainToInstance(ExampleClass , {largeNumber:10086n})[0];

Expected behavior

It should convert the object successfully.

Actual behavior

It crashed.

diffy0712 commented 4 months ago

Hello @Slarper,

I am unable to reproduce your error with the example you've provided. It works for me as expected.

What environment you run this in? ts version and ts target?

Does it work for you if you add the @Type(() => BigInt) type decorator on top of your bigint field?

Also why do you have [0]; at the end of your plainToInstance? It will not return an array.

github-actions[bot] commented 3 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.