TypeScript: conditional default type error
See original GitHub issueDo 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
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Typescript conditional return type with default argument value
this throws an error Type 'true' is not assignable to type 'T'. 'true' is assignable to the constraint of type 'T', but 'T'...
Read more >Documentation - Conditional Types - TypeScript
Conditional types help describe the relation between the types of inputs and outputs. ... In this example, TypeScript errors because T isn't known...
Read more >Default parameter not accepted for conditional type although ...
Expected behavior: No type error (other than unused parameters). Note: This could be two separate bugs. ... Example 2: Even with [...] ?...
Read more >3 Ways to Set Default Value in JavaScript | SamanthaMing.com
Let's break down the 3 different ways to set Default Values using logical operator, ternary, and if/else...
Read more >TypeScript Default Parameters
In this tutorial, you will learn about the TypeScript default parameters to set a value that a parameter will be assigned if it...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I can reproduce the issue in one of the projects that I use TS on.
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.