Skip to content

Incorrect inferred return type for distinct() on array fields #14026

Closed
@richardsimko

Description

@richardsimko

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.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 be string[][] for a string[] field when in fact it should be string[]

Steps to Reproduce

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' ]

distinctBar is actually a string[] but the inferred return type is string[][].

Expected Behavior

Inferred return type does not add Array to a type which is already an Array

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.typescriptTypes or Types-test related issue / Pull Request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions