diff --git a/test/types/utility.test.ts b/test/types/utility.test.ts index 03dc3d7e9af..8820d88b19d 100644 --- a/test/types/utility.test.ts +++ b/test/types/utility.test.ts @@ -1,4 +1,4 @@ -import { MergeType } from 'mongoose'; +import { MergeType, WithTimestamps } from 'mongoose'; import { expectType } from 'tsd'; type A = { a: string, c: number }; @@ -11,3 +11,21 @@ expectType({} as MergeType['c']); expectType({} as MergeType['a']); expectType({} as MergeType['b']); expectType({} as MergeType['c']); + +type C = WithTimestamps<{ a: string; b: string }>; +expectType({} as C['a']); +expectType({} as C['b']); +expectType({} as C['createdAt']); +expectType({} as C['updatedAt']); + +type D = WithTimestamps< + { a: string; b: string }, + { + createdAt: 'created'; + updatedAt: 'modified'; + } +>; +expectType({} as D['a']); +expectType({} as D['b']); +expectType({} as D['created']); +expectType({} as D['modified']); diff --git a/types/utility.d.ts b/types/utility.d.ts index 7c6df561818..145e5571613 100644 --- a/types/utility.d.ts +++ b/types/utility.d.ts @@ -93,4 +93,12 @@ type AddThisParameter = { : T[K]; }; + /** + * @summary Adds timestamp fields to a type + * @description Adds createdAt and updatedAt fields of type Date, or custom timestamp fields if specified + * @param {T} T The type to add timestamp fields to + * @param {P} P Optional SchemaTimestampsConfig or boolean to customize timestamp field names + * @returns T with timestamp fields added + */ + export type WithTimestamps = ResolveTimestamps; }