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

Document does not type non-default timestamp property names #13215

Open
2 tasks done
maxmwang opened this issue Mar 26, 2023 · 2 comments
Open
2 tasks done

Document does not type non-default timestamp property names #13215

maxmwang opened this issue Mar 26, 2023 · 2 comments
Labels
typescript Types or Types-test related issue / Pull Request

Comments

@maxmwang
Copy link

maxmwang commented Mar 26, 2023

Prerequisites

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

Mongoose version

6.8.0, 7.0.3

Node.js version

16.17.0

MongoDB server version

5.0.14

Typescript version (if applicable)

4.8.4

Description

Builds on earlier issue #12069 where Document and InferSchemaType does not infer timestamp fields into Typescript types.

Renamed timestamp fields are not extracted using Document and InferSchemaType.

Steps to Reproduce

import { Schema, InferSchemaType, Document } from 'mongoose';

const MySchema = new Schema({ 
  username: String,
}, {
  timestamps: {
    createdAt: "date_joined",
    updatedAt: false,
  },
});

export type MyType = Document & InferSchemaType<typeof MySchema>;

Expected Behavior

We expect MyType to be:

type MyType = {
  username: String,
  date_joined: Date,
};

We get:

type MyType = {
  username: String,
};

Screen Shot 2023-03-26 at 12 58 09 AM

Note how not even createdAt is on the type. Looking at the actual Document, we will see a type that looks like our expected type (with username, date_joined, and no updatedAt field).

@vkarpov15
Copy link
Collaborator

We'll add support for this in the future. But, in the meantime, I recommend you add date_joined to your schema manually as follows.

const MySchema = new Schema({ 
  username: String,
  date_joined: Date,
}, {
  timestamps: {
    createdAt: "date_joined",
    updatedAt: false,
  },
});

@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Mar 28, 2023
@vkarpov15 vkarpov15 added this to the TypeScript backlog milestone Mar 28, 2023
@donoftime2018
Copy link

The way I added my timestamps is as follows:

const MySchema = new Schema({
username: String,
date_joined: Date,
}, {
timestamps: true
});

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
Development

No branches or pull requests

3 participants