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

Typescript: schema type required argument, and this type #13797

Closed
2 tasks done
AbdelrahmanHafez opened this issue Aug 29, 2023 · 0 comments · Fixed by #13851
Closed
2 tasks done

Typescript: schema type required argument, and this type #13797

AbdelrahmanHafez opened this issue Aug 29, 2023 · 0 comments · Fixed by #13851
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@AbdelrahmanHafez
Copy link
Collaborator

AbdelrahmanHafez commented Aug 29, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.5.0

Node.js version

18.x

MongoDB server version

N/A

Typescript version (if applicable)

5.1.6

Description

In JS world, the document is accessible as an argument and as this for required functions.
In TS, however, we can not access those types.

Steps to Reproduce

The following test fails

function gh13797() {
  interface User {
    name: string;
    age: number;
  }

  const schemaWithArrowFunction = new Schema<User>({
    age: Number,
    name: {
      type: String,
      required(user) {
        expectType<User>(user);
        expectType<number>(user.age);
        return user.age > 30;
      }
    }
  });

  const schemaWithRegularFunction = new Schema<User>({
    age: Number,
    name: {
      type: String,
      required: function (user) {
        expectType<User>(user);
        expectType<User>(this);
        expectType<number>(user.age);
        expectType<number>(this.age);

        return user.age > 30;
      }
    }
  });
}

Expected Behavior

I expect it to pass.

@AbdelrahmanHafez AbdelrahmanHafez added the typescript Types or Types-test related issue / Pull Request label Aug 29, 2023
@vkarpov15 vkarpov15 added this to the 7.5.1 milestone Aug 30, 2023
@vkarpov15 vkarpov15 modified the milestones: 7.5.1, 7.5.3 Sep 8, 2023
@vkarpov15 vkarpov15 modified the milestones: 7.5.3, 7.5.2 Sep 14, 2023
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

Successfully merging a pull request may close this issue.

2 participants