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

[bug?] mongoose and typescript casting #10735

Closed
AbdelrahmanHafez opened this issue Sep 16, 2021 · 1 comment
Closed

[bug?] mongoose and typescript casting #10735

AbdelrahmanHafez opened this issue Sep 16, 2021 · 1 comment
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@AbdelrahmanHafez
Copy link
Collaborator

AbdelrahmanHafez commented Sep 16, 2021

The following script doesn't compile, and errors out on the line where we Shop.find, complaining about the filter type.

import mongoose from 'mongoose';
const { Schema } = mongoose;

const shopSchema = new Schema<IShop>({
  brandId: { type: Schema.Types.ObjectId, ref: 'Business' }
});

const Shop = mongoose.model<IShop>('Shop', shopSchema);
run().catch(console.error);

async function run() {
  // passes if we change it to: { $in: [new mongoose.Types.ObjectId('0'.repeat(24))] }
  await Shop.find({ brandId: { $in: ['0'.repeat(24)] } });

  console.log('Done.');
}

interface IShop {
  brandId: mongoose.Types.ObjectId
}

The error:

No overload matches this call.
  Overload 1 of 3, '(callback?: Callback<(Document<any, any, IShop> & IShop & { _id: ObjectId; })[]>): Query<(Document<any, any, IShop> & IShop & { ...; })[], Document<...> & ... 1 more ... & { ...; }, {}, IShop>', gave the following error.
    Argument of type '{ brandId: { $in: string[]; }; }' is not assignable to parameter of type 'Callback<(Document<any, any, IShop> & IShop & { _id: ObjectId; })[]>'.
      Object literal may only specify known properties, and 'brandId' does not exist in type 'Callback<(Document<any, any, IShop> & IShop & { _id: ObjectId; })[]>'.
  Overload 2 of 3, '(filter: FilterQuery<IShop>, callback?: Callback<IShop[]>): Query<(Document<any, any, IShop> & IShop & { _id: ObjectId; })[], Document<...> & ... 1 more ... & { ...; }, {}, IShop>', gave the following error.
    Type 'string' is not assignable to type 'ObjectId'.
  Overload 3 of 3, '(filter: FilterQuery<IShop>, projection?: any, options?: QueryOptions, callback?: Callback<(Document<any, any, IShop> & IShop & { ...; })[]>): Query<...>', gave the following error.
    Type 'string' is not assignable to type 'ObjectId'.ts(2769)

This may be expected behavior since we're passing stringified values of object ids, but then again in JS, mongoose is nice enough to cast those ids out of the box for us, but that's not the case for TypeScript.

Since casting is one of the main selling points of mongoose, I think we should include the same behavior in Typescript.
Quoting the mongoose homepage:

writing MongoDB validation, casting and business logic boilerplate is a drag.

What do you think?
@vkarpov15 @taxilian @IslandRhythms

P.S: this used to work with mongoose 5.x.

@teolag
Copy link

teolag commented Sep 16, 2021

I was just about to post the same issue.

function findById(companyId: string) {
  return Company.findById(companyId) // No complains finding ObjectIds using a string
}

function findByStringId(companyId: string) {
  return Company.find({_id: companyId}) // No complains finding ObjectIds using a string
}

function findByIds(companyIds: string[]) {
  return Company.find({_id: {$in: companyIds}})  // No overload matches this call... Type 'string[]' is not assignable to type 'ObjectId[]'.
}

If this was intended, I think the first two examples would be invalid as well?

@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Sep 16, 2021
@vkarpov15 vkarpov15 added this to the 6.0.7 milestone Sep 18, 2021
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

No branches or pull requests

4 participants