-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Incorrect inferred return type for distinct()
on array fields
#14026
Comments
distinct is actually returning an array of String arrays. bar is a String[] and distinct selects all unique String arrays within an array. So since distinct is returning an array of String arrays with one item it'll look something like this: [ |
@donoftime2018 No, Here's an amended example: interface Foo {
bar: string[];
}
const FooModel = model<Foo>('Foo', new Schema<Foo>({ bar: [String] }));
await FooModel.create({ bar: ['a', 'b'] })
const distinctBar = await FooModel.distinct('bar');
console.log(distinctBar); // [ 'a', 'b' ] I've edited the description as well to explain this |
Try adding more documents to the collection so you can see what I am talking about or updating your version of mongoose |
Upon further testing comes back as import * as mongoose from 'mongoose';
interface Foo {
bar: string[];
}
const FooModel = mongoose.model<Foo>('Foo', new mongoose.Schema<Foo>({ bar: [String] }));
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await FooModel.create({ bar: ['a', 'b'] })
await FooModel.create({ bar: ['c','d']});
await FooModel.create({ bar: ['e','f']})
const distinctBar = await FooModel.distinct('bar');
console.log(distinctBar); // [ 'a', 'b', 'c', 'd', 'e', 'f' ]
}
run(); |
Shall I report this as a bug? |
@donoftime2018 That does sound like a separate bug, yes. I couldn't find anything in neither the mongo nor mongoose docs pointing to the behavior you mentioned being correct. As @IslandRhythms mentioned the type is |
Np |
@donoftime2018 please open a new issue and follow the template if you have a bug to report. |
How? |
From this page on the top right next to this issue title you should see a new issue button. |
I wanna take a go and fix this issue via code so what should I do? |
import * as mongoose from 'mongoose'; interface Foo { const FooModel = mongoose.model('Foo', new mongoose.Schema({ bar: [String], bar_num: [Number] })); async function run() { const distinctBar = await FooModel.distinct('bar'); run(); The inferred types are String[] and Number[] after removing the array brackets from interface Foo |
types(model+query): unpack arrays in distinct return type
Prerequisites
Mongoose version
8.0.0
Node.js version
20.0.0
MongoDB server version
6.0
Typescript version (if applicable)
latest
Description
The inferred return type for
distinct()
queries with an array fields is incorrect. It's inferred to bestring[][]
for astring[]
field when in fact it should bestring[]
Steps to Reproduce
distinctBar
is actually astring[]
but the inferred return type isstring[][]
.Expected Behavior
Inferred return type does not add
Array
to a type which is already anArray
The text was updated successfully, but these errors were encountered: