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

Mongoose build error with latest TypeScript 'Type of property 'defaultProps' circularly references itself in mapped type' #9920

Closed
NenadJovicic opened this issue Feb 11, 2021 · 1 comment
Assignees
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary typescript Types or Types-test related issue / Pull Request

Comments

@NenadJovicic
Copy link

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

What is the current behavior?
I create Document for Schema and I type some property as that Document. Then I get a build error that type got a reference to itself. Same issue with some other libraries could be found here microsoft/TypeScript#37597

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

import { Document, HookErrorCallback, Schema } from "mongoose";

export const UserSchema: Schema = new Schema({
  friend: {
    type: Schema.Types.ObjectId,
    ref: "user",
  },
  someString: {
    type: Schema.Types.String,
  },
  updatedAt: {
    type: Schema.Types.Date,
  },
});

UserSchema.pre("save", function (next: HookErrorCallback): any {
  (this as UserDoc).updatedAt = new Date(); // on this line, it throws an error that `friend` circularly reference itself
  return next();
});

// if I don't extend `Document` there is no build error, so it is linked with mongoose.Document
export interface UserDoc extends Document {
  // typing it as UserDoc because it would be populated
  friend: UserDoc;
  someString: string;
  updatedAt: Date;
}

This is tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "declaration": false,
        "noImplicitAny": false,
        "removeComments": true,
        "noLib": false,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "target": "es6",
        "sourceMap": true,
        "allowJs": true,
        "outDir": "./build",
        "baseUrl": "./src"
    },
    "include": ["."],
    "exclude": ["./node_modules"]
}

This is package.json file

{
  "name": "mongoose-typescript-error",
  "version": "1.0.0",
  "description": "Demo project to provide example of error with typescript",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "tsc"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "mongoose": "5.11.11"
  },
  "devDependencies": {
    "ts-node": "9.1.1",
    "typescript": "4.1.3"
  }
}

It is enough to run npm run build and to get an error in terminal
What is the expected behavior?
Expected behavior is that build will pass without any errors.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js: 12.13.0
Mongoose: 5.11.11 (tried also with 5.11.15, same thing)
MongoDB: 3.6

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue typescript Types or Types-test related issue / Pull Request labels Feb 11, 2021
@vkarpov15 vkarpov15 modified the milestones: 5.11.17, 5.11.18 Feb 15, 2021
@vkarpov15
Copy link
Collaborator

We'll likely have a better approach to this with #9818, but the workaround here is to use a union type because friend can be either a user or an ObjectId:

import { Document, HookErrorCallback, Schema, Types } from "mongoose";
  
export const UserSchema: Schema = new Schema({
  friend: {
    type: Schema.Types.ObjectId,
    ref: "user",
  },
  someString: {
    type: Schema.Types.String,
  },
  updatedAt: {
    type: Schema.Types.Date,
  },
});

UserSchema.pre("save", function (next: HookErrorCallback): any {
  (this as UserDoc).updatedAt = new Date();
  ((this as UserDoc).friend as UserDoc).someString.trim();
  return next();
});

export interface UserDoc extends Document {
  friend: Types.ObjectId | UserDoc;
  someString: string;
  updatedAt: Date;
}

@vkarpov15 vkarpov15 removed this from the 5.11.18 milestone Feb 22, 2021
@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Feb 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

3 participants