Skip to content

Typescript sees the Boolean Constructor Type as Date when using Mongoose  #14630

Closed

Description

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.4.0

Node.js version

21.7.1

MongoDB server version

6.6.2

Typescript version (if applicable)

5.4.5

Description

Whenever I try to make an Schema with a property of the Type Boolean Constructor the typescript server sees it as being of the type Date.

error TS2322: Type 'boolean' is not assignable to type 'Date'.

22 course.isPublished = false;
~~~~~~~~~~~~~~~~~~

Found 1 error in src/updatemongo.ts:22

Steps to Reproduce

I just create a TypeScript project with this tsconfig

{
"compilerOptions": {
"target": "ES2015",
"module": "commonjs",
"esModuleInterop": true,
"moduleResolution": "Node",
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"rootDir": "src",
"outDir": "dist"
}
}

install the latest mongoose available using npm and
try to make an Scheme like this.

import mongoose, { Mongoose } from "mongoose";
const courseSchema = new mongoose.Schema({
name: String,
author: String,
tags: [String],
date: { type: Date, default: Date.now },
prices:Number,
isPublished: Boolean
});

const Course = mongoose.model('Course', courseSchema);

async function updateCourse(id:any) {
const course = await Course.findById(id);
if (!course) return;
course.isPublished = false; //tsc won't compile because it will see it as being of type Date
course.author = 'Another Author';
}

updateCourse("none");

BooleanASDate

Expected Behavior

Typescript should see it as being of type Boolean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    typescriptTypes or Types-test related issue / Pull Request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions