-
Notifications
You must be signed in to change notification settings - Fork 218
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
Add support for BigInt #474
Comments
Thanks for issue. It will be supported in next version. |
Amazing! One step closer to the Nuxter role! |
@pi0 Hello, thought this might help providing some info to your work. I am using [email protected] and it seems to still have the same issue..
Thank you for any tips |
The issue with serializing BigInt values within objects or arrays remains unresolved. Here's an example of an error encountered with [email protected]:
It appears that the serialization of BigInt values, particularly when nested within objects or arrays, remains problematic. Alternative solution for the moment that I propose: type StringifyBigInt<T> = T extends bigint
? string
: T extends (infer U)[]
? StringifyBigInt<U>[]
: T extends object
? { [K in keyof T]: StringifyBigInt<T[K]> }
: T;
export function stringifyBigInt<T>(body: T): StringifyBigInt<T> {
if (typeof body !== 'object' || body === null) {
return (typeof body === 'bigint' ? body.toString() : body) as StringifyBigInt<T>;
}
if (Array.isArray(body)) {
return body.map(stringifyBigInt) as StringifyBigInt<T>;
}
return Object.fromEntries(
Object.entries(body).map(([key, value]) => [key, stringifyBigInt(value)]),
) as StringifyBigInt<T>;
} |
Your solution is not supporting Dates, here my updated version. type StringifyBigInt<T> = T extends bigint
? string
: T extends Date
? Date
: T extends (infer U)[]
? StringifyBigInt<U>[]
: T extends object
? { [K in keyof T]: StringifyBigInt<T[K]> }
: T;
export function stringifyBigInt<T>(body: T): StringifyBigInt<T> {
if (typeof body === 'bigint') {
return body.toString() as StringifyBigInt<T>;
}
if (body instanceof Date || typeof body !== 'object' || body === null) {
return body as StringifyBigInt<T>;
}
if (Array.isArray(body)) {
return body.map(stringifyBigInt) as StringifyBigInt<T>;
}
return Object.fromEntries(
Object.entries(body).map(([key, value]) => [key, stringifyBigInt(value)])
) as StringifyBigInt<T>;
} |
Describe the feature
Currently if a BigInt needs to be sent to the client an error will be thrown.
Would love to see support for this!
One possible solution would be to use the reviver function. However I understand this would add complexity to both the sever and client. Wondering what the teams thoughts are regarding this functionality?
https://stackoverflow.com/questions/62539236/how-to-parse-a-json-data-type-bigint-in-typescript
Additional information
The text was updated successfully, but these errors were encountered: