Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion test/types/utility.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MergeType } from 'mongoose';
import { MergeType, WithTimestamps } from 'mongoose';
import { expectType } from 'tsd';

type A = { a: string, c: number };
Expand All @@ -11,3 +11,21 @@ expectType<number>({} as MergeType<B, A>['c']);
expectType<number>({} as MergeType<A, B>['a']);
expectType<string>({} as MergeType<A, B>['b']);
expectType<number>({} as MergeType<A, B>['c']);

type C = WithTimestamps<{ a: string; b: string }>;
expectType<string>({} as C['a']);
expectType<string>({} as C['b']);
expectType<Date>({} as C['createdAt']);
expectType<Date>({} as C['updatedAt']);

type D = WithTimestamps<
{ a: string; b: string },
{
createdAt: 'created';
updatedAt: 'modified';
}
>;
expectType<string>({} as D['a']);
expectType<string>({} as D['b']);
expectType<Date>({} as D['created']);
expectType<Date>({} as D['modified']);
8 changes: 8 additions & 0 deletions types/utility.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,12 @@ type AddThisParameter<T, D> = {
: 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<T, P extends SchemaTimestampsConfig | boolean = true> = ResolveTimestamps<T, { timestamps: P }>;
}
Loading