question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript: conditional default type error

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
AbdelrahmanHafezcommented, May 30, 2022

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

0reactions
vkarpov15commented, 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:

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found