-
-
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: timestamps not inferred from schema #12069
Comments
I am also converting a project to TS and experiencing this, however, an explicit interface seems to cause the same issue. import { model, Schema } from "mongoose"
interface IPost {
title: string
}
const postSchema = new Schema(
{
title: {
type: String,
required: true,
},
},
{
timestamps: true,
}
)
const Post = model<IPost>("Post", postSchema)
Post.create({ title: "foo" }).then((post) => {
console.log(post._id)
console.log(post.createdAt) // property 'createdAt' does not exist on type 'Document<unknown, any, IPost> & IPost & { _id: ObjectId; }'.
}) Being a bit of TS noob, is there a workaround that does not require @ts-expect-error everywhere? It is reproducible in every 6.x release I tried. |
I know this isn't ideal but might help with your typescript errors until this is fixed in the next milestone? interface IPost {
title: string
createdAt: Date;
updateAt: Date;
}
const postSchema = new Schema<IPost>(
{
title: {
type: String,
required: true,
},
},
{
timestamps: true,
}
)
const Post = model<IPost>("Post", postSchema)
Post.create({ title: "foo" }).then((post) => {
console.log(post._id)
console.log(post.createdAt) // (property) IPost.createdAt: Date
}) |
Experiencing the same issue. Looking forward to a fix! |
Could you try to access with following |
Doesn't make a difference |
@janickvwcotiss @neocameback |
Infer timestamps option from schema
Personally I just solved mine by delegating the object TYPE ( i.e Object variable not the model itself) to ANY instead of ObjectId in my code |
Prerequisites
Mongoose version
6.4.0
Node.js version
18.2.0
MongoDB server version
5.0.9
Description
Hi there, my issue is to do with typescript.
The issue I am having is that when creating a document schema with timestamps enabled the inferred document type doesn't have the corresponding
createdAt
andupdatedAt
fields present.This means that when querying documents through the schema typescript will throw the property doesn't exist on type issue for the
createdAt
andupdatedAt
fields.Hope this is pretty simple to understand, I've done my best to proved a good code example to best explain the issue I am having.
I realised mongoose has just recently migrated to typescript and isn't fully feature complete yet so appreciate any help.
P.s. I realise I haven't manually typed the interface for the schema e.g.
new Schema<IDocument>
andexport const Document = model<IDocument>('Document', DocumentSchema);
this is because we are migrating our whole code base to typescript and would ideally like to save some time by usingInferSchemaType
. But obviously open to any ideas even if it'll end up requiring typing the interfaces manually.Thanks.
Steps to Reproduce
Expected Behavior
Expect the Document to have the timestamp fields present on the returned type.
The text was updated successfully, but these errors were encountered: