-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Comments
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 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:
|
This is the full code I have and not only the short snippet to get to the error updatemongo.ts
Both the code editor and the tsc command line would tell me that the data type is of type Date when using TypeScript. 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: Let me paste the package.json and the package-lock.json, and the tsconfig.json package.json And I am sure I am on the versions of mongo, tsc and mongoose I placed at the start of the ticket. |
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 Strangely enough, this error doesn't show up in TypeScript Playground either. |
As a workaround, you can use |
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. |
We're still working on figuring out this issue, |
types: avoid inferring Boolean, Buffer, ObjectId as Date in schema definitions under certain circumstances
Prerequisites
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");
Expected Behavior
Typescript should see it as being of type Boolean
The text was updated successfully, but these errors were encountered: