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

Type is incorrect for Query.distinct when field is a nested field #14615

Closed
2 tasks done
bpabel opened this issue May 24, 2024 · 1 comment · Fixed by #14632
Closed
2 tasks done

Type is incorrect for Query.distinct when field is a nested field #14615

bpabel opened this issue May 24, 2024 · 1 comment · Fixed by #14632
Milestone

Comments

@bpabel
Copy link

bpabel commented May 24, 2024

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.1.2

Node.js version

18.x

MongoDB server version

4.2.8

Typescript version (if applicable)

No response

Description

When using the distinct query for nested fields, the declared type of the return value is unknown[], even though the type should be statically discoverable as string[].

A minimal example

myrecord.schema.ts

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';


@Schema()
export class Foo {
  @Prop()
  one: string;
}

export const FooSchema = SchemaFactory.createForClass(Foo);


@Schema()
export class MyRecord extends Document {

  @Prop()
  _id: string;

  @Prop({ type: FooSchema })
  foo: Foo;

export const MyRecordSchema = SchemaFactory.createForClass(MyRecord);

myrecord.service.ts

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { MyRecord } from '../schemas/myrecord.schema';

@Injectable()
export class MyService {
  constructor(
    @InjectModel('myrecord') private model: Model<MyRecord>,
  ) {}

  async myQuery(): Promise<void> {
    const foos: string[] = await this.model.distinct('foo.one').exec()
  }

}

The type checking error shows:

Type 'unknown[]' is not assignable to type 'string[]'

Steps to Reproduce

I've included some sample code above. It's written in the style of nestjs, so it may need to be modified a bit for a simpler use case without nestjs.

  1. Create a document schema with a subdocument.
  2. Write a distinct query for the document on one of the nested fields on the subdocument.
  3. The specified return type will be unknown[]

Expected Behavior

The resolved return type should be an array of the subdocument field type. In this case, string[].

@bpabel bpabel changed the title Type is incorrect when for Query.distinct when field is a nested field Type is incorrect for Query.distinct when field is a nested field May 24, 2024
@vkarpov15
Copy link
Collaborator

Confirmed that this is an issue. We'll investigate and see if we can fix this. In the meantime, you can add a type override as follows to prevent this compiler error.

const foos: string[] = await this.model.distinct<'foo.one', string>('foo.one').exec()

@vkarpov15 vkarpov15 added this to the 8.4.2 milestone May 31, 2024
vkarpov15 added a commit that referenced this issue Jun 4, 2024
types(models+query): infer return type from schema for 1-level deep nested paths
@vkarpov15 vkarpov15 modified the milestones: 8.4.2, 8.5 Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants