Closed
Description
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
Metadata
Metadata
Assignees
Labels
Type
Projects
Relationships
Development
No branches or pull requests
Activity
Uzlopak commentedon May 30, 2022
I cant reproduce this bug
Actually the typing for default is pretty wide.
Note: changed "can" to "can't"
ahmedelshenawy25 commentedon May 30, 2022
@Uzlopak I'm confused. Can you or can you not reproduce it?
Uzlopak commentedon May 30, 2022
sry, I meant I can not reproduce.
ahmedelshenawy25 commentedon May 30, 2022
@AbdelrahmanHafez @IslandRhythms can you reproduce it?
AbdelrahmanHafez commentedon May 30, 2022
I can reproduce the issue in one of the projects that I use TS on.
test(typescript): repro #11828
vkarpov15 commentedon Jun 11, 2022
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:
gets the below error:
Why TypeScript thinks the function type is
(this: any, doc: any) => false
is beyond me. Removing theT extends Schema.Types.Mixed
conditional works, as does the fix I put in.