Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 tasks done
molina95 opened this issue May 31, 2024 · 6 comments · Fixed by #14667
Closed
2 tasks done

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

molina95 opened this issue May 31, 2024 · 6 comments · Fixed by #14667
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@molina95
Copy link

molina95 commented May 31, 2024

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

@molina95 molina95 changed the title Mongoose sees the Boolean Constructor Type as Date when using TypeScript Typescript sees the Boolean Constructor Type as Date when using Mongoose May 31, 2024
@vkarpov15
Copy link
Collaborator

I'm unable to repro. The following script compiles correctly with Mongoose 8.4.0 and TypeScript 5.4.5. Please modify the following script to demonstrate your issue. Also, just to be clear, do you get this error when you compile TypeScript, or does this error just show up when you hover over isPublished in your code editor?

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');

Output:

$ ./node_modules/.bin/tsc --strict --target ES2015 --moduleResolution node --esModuleInterop gh-14630.ts 
$ head node_modules/typescript/package.json 
{
    "name": "typescript",
    "author": "Microsoft Corp.",
    "homepage": "https://www.typescriptlang.org/",
    "version": "5.4.5",
    "license": "Apache-2.0",
    "description": "TypeScript is a language for application scale JavaScript development",
    "keywords": [
        "TypeScript",
        "Microsoft",
$ 

@vkarpov15 vkarpov15 added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Jun 2, 2024
@molina95
Copy link
Author

molina95 commented Jun 3, 2024

I'm unable to repro. The following script compiles correctly with Mongoose 8.4.0 and TypeScript 5.4.5. Please modify the following script to demonstrate your issue. Also, just to be clear, do you get this error when you compile TypeScript, or does this error just show up when you hover over isPublished in your code editor?

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');

Output:

$ ./node_modules/.bin/tsc --strict --target ES2015 --moduleResolution node --esModuleInterop gh-14630.ts 
$ head node_modules/typescript/package.json 
{
    "name": "typescript",
    "author": "Microsoft Corp.",
    "homepage": "https://www.typescriptlang.org/",
    "version": "5.4.5",
    "license": "Apache-2.0",
    "description": "TypeScript is a language for application scale JavaScript development",
    "keywords": [
        "TypeScript",
        "Microsoft",
$ 

This is the full code I have and not only the short snippet to get to the error

updatemongo.ts

import mongoose, { Mongoose } from "mongoose";

const uri = 'mongodb+srv://<user>:<password>@<mongo>/mongo-exercises?retryWrites=true&w=majority&appName=Molina1';
mongoose.connect(uri)
    .then(() => console.log('Connected to Mongo Database...'))
    .catch(err => console.error('Could not connect to Mongo Database...', err));

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 as any;
    course.author = 'Another Author';
    course.isPublished = true;

    course.set({
        isPublished:true,
        author:'Another Author'
    }); 

    const result = await course.save();
    console.log(result);
} 

/*async function updateCourse(id:any) {
    const course = await Course.updateOne({_id:id},{
        $set:{
            author:'Mosh',
            isPublished:true,
        },
        $currentDate:{
            date:true
        }
    });
    
    console.log(course);
}*/

updateCourse("665a0ee68b40030cbec8dd57"); 

Both the code editor and the tsc command line would tell me that the data type is of type Date when using TypeScript.
This is the message I get from tsc

tsc
src/updatemongo.ts:24:5 - error TS2322: Type 'boolean' is not assignable to type 'Date'.

24     course.isPublished = true;
       ~~~~~~~~~~~~~~~~~~

Found 1 error in src/updatemongo.ts:24

And this is the error I get on the IDE from the typescript server:
image

Let me paste the package.json and the package-lock.json, and the tsconfig.json
It may help

package.json
package-lock.json
tsconfig.json

And I am sure I am on the versions of mongo, tsc and mongoose I placed at the start of the ticket.
If there is other info I can give you other than the one I just placed here, please let me know.

@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Jun 12, 2024
@vkarpov15 vkarpov15 added this to the 8.4.2 milestone Jun 12, 2024
@vkarpov15
Copy link
Collaborator

Very strange, this issue doesn't seem to happen if Mongoose is symlinked into node_modules, which is what we often use for debugging. But if you npm install mongoose, this error shows up.

Strangely enough, this error doesn't show up in TypeScript Playground either.

image

@vkarpov15 vkarpov15 added typescript Types or Types-test related issue / Pull Request and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Jun 13, 2024
@vkarpov15
Copy link
Collaborator

As a workaround, you can use isPublished: 'Boolean', that seems to be inferred correctly

@molina95
Copy link
Author

molina95 commented Jun 13, 2024

Thanks that worked for me. but is really weird if you see Typescript playground loads it as date then it settles into boolean, but adding the simple quotes worked fine.

@vkarpov15 vkarpov15 reopened this Jun 13, 2024
@vkarpov15
Copy link
Collaborator

We're still working on figuring out this issue, isPublished: Boolean should work. Just providing a temporary workaround so you're not blocked on us :)

vkarpov15 added a commit that referenced this issue Jun 17, 2024
types: avoid inferring Boolean, Buffer, ObjectId as Date in schema definitions under certain circumstances
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
2 participants