nestjs / mongoose

Mongoose module for Nest framework (node.js) 🍸
https://nestjs.com
MIT License
530 stars 118 forks source link

type unknown and any #2200

Closed wedreamer closed 4 months ago

wedreamer commented 4 months ago

Is there an existing issue for this?

Current behavior

image

image

@Schema({
  toJSON: toJson,
  timestamps: true,
})
export class SampleType extends BaseOrgSchema {
  @Prop({ required: true })
  name!: string; 
}

export const SampleTypeSchema = SchemaFactory.createForClass(SampleType);
export type SampleTypeDocument = HydratedDocument<SampleType>;

tsconfig

{
  "compilerOptions": {
    "module": "NodeNext",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "ES2021",
    "strict": true,
    "esModuleInterop": true,
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictBindCallApply": true,
    "forceConsistentCasingInFileNames": false,
    "paths": {
      "@app/mongo": [
        "libs/mongo/src"
      ],
      "@app/mongo/*": [
        "libs/mongo/src/*"
      ]
    }
  }
}

Minimum reproduction code

https://stackblitz.com/edit/node-rsvpt7?file=src%2Fcat%2Fcat.controller.ts

Steps to reproduce

run

nest new nestjs-mongoose-type-miss
cd nestjs-mongoose-type-miss
npm i
npm i @nestjs/mongoose mongoose
nest g mo cat
nest g co cat

see src/cat/cat.controller.ts find method.

Expected behavior

public toFilter() {
    const filter: FilterQuery<SampleTypeDocument> = {};

    if (this.code) filter.code = this.code; // filter.code has type
    if (this.category) filter.category = this.category; // same below
    if (this.storageType) filter.storageType = this.storageType; // same below
    if (this.name) filter.name = this.name; // same below
    // has ts error
    filter.xxx = {};
    return filter;
  }

Package version

^10.0.10

mongoose version

^8.5.0

NestJS version

^10.0.0

Node.js version

v20.11.0

In which operating systems have you tested?

Other

No response

kamilmysliwiec commented 4 months ago

Thank you for taking the time to submit your report! From the looks of it, this could be better discussed on our Discord. If you haven't already, please join here and send a new post in the #⁠ 🐈 nestjs-help forum. Make sure to include a link to this issue, so you don't need to write it all again. We have a large community of helpful members, who will assist you in getting this to work.