Skip to content

Commit 3e0c645

Browse files
authored
Merge pull request #15318 from baruchiro/types/with-timestamps
feat(types): add WithTimestamps utility type
2 parents e74d5e5 + bdf38fc commit 3e0c645

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

test/types/utility.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MergeType } from 'mongoose';
1+
import { MergeType, WithTimestamps } from 'mongoose';
22
import { expectType } from 'tsd';
33

44
type A = { a: string, c: number };
@@ -11,3 +11,21 @@ expectType<number>({} as MergeType<B, A>['c']);
1111
expectType<number>({} as MergeType<A, B>['a']);
1212
expectType<string>({} as MergeType<A, B>['b']);
1313
expectType<number>({} as MergeType<A, B>['c']);
14+
15+
type C = WithTimestamps<{ a: string; b: string }>;
16+
expectType<string>({} as C['a']);
17+
expectType<string>({} as C['b']);
18+
expectType<Date>({} as C['createdAt']);
19+
expectType<Date>({} as C['updatedAt']);
20+
21+
type D = WithTimestamps<
22+
{ a: string; b: string },
23+
{
24+
createdAt: 'created';
25+
updatedAt: 'modified';
26+
}
27+
>;
28+
expectType<string>({} as D['a']);
29+
expectType<string>({} as D['b']);
30+
expectType<Date>({} as D['created']);
31+
expectType<Date>({} as D['modified']);

types/utility.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,12 @@ type AddThisParameter<T, D> = {
9393
: T[K];
9494
};
9595

96+
/**
97+
* @summary Adds timestamp fields to a type
98+
* @description Adds createdAt and updatedAt fields of type Date, or custom timestamp fields if specified
99+
* @param {T} T The type to add timestamp fields to
100+
* @param {P} P Optional SchemaTimestampsConfig or boolean to customize timestamp field names
101+
* @returns T with timestamp fields added
102+
*/
103+
export type WithTimestamps<T, P extends SchemaTimestampsConfig | boolean = true> = ResolveTimestamps<T, { timestamps: P }>;
96104
}

0 commit comments

Comments
 (0)