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: conditional default type error #11828

Closed
ahmedelshenawy25 opened this issue May 24, 2022 · 6 comments
Closed

TypeScript: conditional default type error #11828

ahmedelshenawy25 opened this issue May 24, 2022 · 6 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@ahmedelshenawy25
Copy link
Contributor

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

What is the current behavior?

Type '{ type: BooleanConstructor; default(this: any): boolean; }' is not assignable to type 'SchemaDefinitionProperty<boolean>'.
  Type '{ type: BooleanConstructor; default(this: any): boolean; }' is not assignable to type 'false'.ts(2322)
index.ts(17, 3): The expected type comes from property 'isPet' which is declared here on type '{ name?: SchemaDefinitionProperty<string>; isPet?: SchemaDefinitionProperty<boolean>; }'

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

import mongoose from 'mongoose';
const { Schema } = mongoose;

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test');

  const animalSchema = new Schema<IAnimal>({
    name: String,
    isPet: { type: Boolean, default() { return this.name === 'foo' } }
  });

  const Animal = mongoose.model<IAnimal>('Animal', animalSchema);
}

interface IAnimal {
  name?: string;
  isPet?: boolean;
}
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "skipLibCheck": true,
  }
}

What is the expected behavior?
No type errors

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

@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label May 26, 2022
@Uzlopak
Copy link
Collaborator

Uzlopak commented May 30, 2022

I cant reproduce this bug


function gh11828() {
  interface IAnimal {
    name?: string;
    isPet?: boolean;
  }
  const animalSchema = new Schema<IAnimal>({
    name: String,
    isPet: { type: Boolean, default() { return this.name === 'foo' } }
  });

  const Animal = model<IAnimal>('Animal', animalSchema);
}

Actually the typing for default is pretty wide.

Note: changed "can" to "can't"

@Uzlopak Uzlopak added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label May 30, 2022
@ahmedelshenawy25
Copy link
Contributor Author

@Uzlopak I'm confused. Can you or can you not reproduce it?

@Uzlopak
Copy link
Collaborator

Uzlopak commented May 30, 2022

sry, I meant I can not reproduce.

@ahmedelshenawy25
Copy link
Contributor Author

@AbdelrahmanHafez @IslandRhythms can you reproduce it?

@AbdelrahmanHafez AbdelrahmanHafez removed the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label May 30, 2022
@AbdelrahmanHafez
Copy link
Collaborator

I can reproduce the issue in one of the projects that I use TS on.

@AbdelrahmanHafez AbdelrahmanHafez added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label May 30, 2022
AbdelrahmanHafez added a commit that referenced this issue Jun 1, 2022
@vkarpov15 vkarpov15 added this to the 6.3.7 milestone Jun 10, 2022
@vkarpov15
Copy link
Collaborator

I took a look, I have a fix but I have not the slightest idea why this issue happens or why my fix works. The issue is that the below TS:

const t: SchemaTypeOptions<boolean> = {
    type: Boolean,
    default() {
      return this.name === 'Hafez';
    }
  };

gets the below error:

Type (this: any) => boolean is not assignable to type boolean | ((this: any, doc: any) => false) | ((this: any, doc: any) => true) | undefined.
  Type (this: any) => boolean is not assignable to type (this: any, doc: any) => false.
    Type boolean is not assignable to type false. 

Why TypeScript thinks the function type is (this: any, doc: any) => false is beyond me. Removing the T extends Schema.Types.Mixed conditional works, as does the fix I put in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

5 participants