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

The this type in middleware hook always resolves to any #11480

Closed
iammola opened this issue Mar 5, 2022 · 1 comment
Closed

The this type in middleware hook always resolves to any #11480

iammola opened this issue Mar 5, 2022 · 1 comment
Labels
typescript Types or Types-test related issue / Pull Request

Comments

@iammola
Copy link
Contributor

iammola commented Mar 5, 2022

Do you want to request a feature or report a bug?
Bug

What is the current behaviour?
I want to use the this type in the pre-save middleware I've added to my schema. But the this always resolves to any with Typescript. I normally read through your index.d.ts file and I know the this type is defined, so I don't like importing extra things from mongoose because of the extra space since it's already there.

The pre method's generic argument is set to be this HydratedDocument you pass other type arguments to.

mongoose/types/index.d.ts

Lines 1194 to 1196 in 18cb0be

pre<T = HydratedDocument<DocType, TInstanceMethods>>(method: 'save', fn: PreSaveMiddlewareFunction<T>): this;
pre<T = HydratedDocument<DocType, TInstanceMethods>>(method: 'save', options: SchemaPreOptions, fn: PreSaveMiddlewareFunction<T>): this;
pre<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PreMiddlewareFunction<T>): this;

The TInstanceMethods type defaults to any, and in the HydratedDocument absorbs the other types and everything resolves to any.

This is the case in many other places and I understand why any is used, but I want to ask if you can consider using unknown or {} for those types you expect to be an object in any case. Like the methods or query helpers. You already do this with the Model definition ⬇️

interface Model<T, TQueryHelpers = {}, TMethodsAndOverrides = {}, TVirtuals = {}> extends NodeJS.EventEmitter, AcceptsDiscriminator {

If the current behaviour is a bug, please provide the steps to reproduce.

import { Schema } from "mongoose";

type IUserSchema = {
	name: string;
}

const UserSchema = new Schema<IUserSchema>({ name: { type: String } });

UserSchema.pre("save", function (next) {
	this.name; // Unsafe member access .name on an `any` value
	next();
});

If you are using TypeScript, please include your tsconfig.json

{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "downlevelIteration": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "incremental": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "lib": ["dom", "dom.iterable", "esnext"],
    "module": "esnext",
    "moduleResolution": "node",
    "noEmit": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "target": "esnext"
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.JS"],
  "exclude": ["node_modules", ".next", "out"]
}

What is the expected behaviour?
The this type to be properly types

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node.JS - 17.6.0
Mongoose - 6.2.4
MongoDB - 4.3.1
@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Mar 7, 2022
@vkarpov15 vkarpov15 added this to the 6.2.8 milestone Mar 11, 2022
vkarpov15 added a commit that referenced this issue Mar 23, 2022
@vkarpov15
Copy link
Collaborator

I confirmed this issue was fixed in v6.2.7 with the fix for #11435. Please upgrade to v6.2.7 for the fix 👍

@vkarpov15 vkarpov15 removed this from the 6.2.9 milestone Mar 23, 2022
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

3 participants