Skip to content

TypeScript: conditional default type error #11828

Closed
@ahmedelshenawy25

Description

@ahmedelshenawy25
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

Activity

Uzlopak

Uzlopak commented on May 30, 2022

@Uzlopak
Collaborator

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"

added
can't reproduceMongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
on May 30, 2022
ahmedelshenawy25

ahmedelshenawy25 commented on May 30, 2022

@ahmedelshenawy25
ContributorAuthor

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

Uzlopak

Uzlopak commented on May 30, 2022

@Uzlopak
Collaborator

sry, I meant I can not reproduce.

ahmedelshenawy25

ahmedelshenawy25 commented on May 30, 2022

@ahmedelshenawy25
ContributorAuthor

@AbdelrahmanHafez @IslandRhythms can you reproduce it?

removed
can't reproduceMongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
on May 30, 2022
AbdelrahmanHafez

AbdelrahmanHafez commented on May 30, 2022

@AbdelrahmanHafez
Collaborator

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

added this to the 6.3.7 milestone on Jun 10, 2022
vkarpov15

vkarpov15 commented on Jun 11, 2022

@vkarpov15
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.typescriptTypes or Types-test related issue / Pull Request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @vkarpov15@Uzlopak@AbdelrahmanHafez@ahmedelshenawy25@IslandRhythms

        Issue actions

          TypeScript: conditional default type error · Issue #11828 · Automattic/mongoose