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

BigInt type doesn't work as expected #14147

Closed
2 tasks done
akim-bow opened this issue Dec 3, 2023 · 2 comments · Fixed by #14160
Closed
2 tasks done

BigInt type doesn't work as expected #14147

akim-bow opened this issue Dec 3, 2023 · 2 comments · Fixed by #14160
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@akim-bow
Copy link

akim-bow commented Dec 3, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.0.2

Node.js version

18.x

MongoDB server version

6.x

Typescript version (if applicable)

5.2.2

Description

When i use BigInt type in mongoose schema, i cannot pass typecheck and assign BigInt value to it's property.

Steps to Reproduce

export const affiliateSchema = new Schema({
  balance: { type: BigInt, default: BigInt(0) },
});

const AffiliateModel = model("Affiliate", affiliateSchema);

AffiliateModel.findOne({})!.then(modelValue => {
  modelValue.balance = 123n;
});

image

Expected Behavior

No type error here.

@akim-bow
Copy link
Author

akim-bow commented Dec 3, 2023

Currently i can fix it with a hacky type like this

export type AffiliateSchema = Omit<InferSchemaType<typeof affiliateSchema>, 'balance'> & {
  balance: bigint;
};

@vkarpov15 vkarpov15 added this to the 8.0.3 milestone Dec 5, 2023
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Dec 5, 2023
@vkarpov15
Copy link
Collaborator

Workaround is to do type: 'BigInt' in your schema as follows:

import mongoose from 'mongoose';

export const affiliateSchema = new mongoose.Schema({
  balance: { type: 'BigInt', default: BigInt(0) },
});

const AffiliateModel = mongoose.model("Affiliate", affiliateSchema);

AffiliateModel.findOne({}).orFail().then(modelValue => {
  modelValue.balance = 123n;
});

vkarpov15 added a commit that referenced this issue Dec 7, 2023
types: handle using BigInt global class in schema definitions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants