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
fix: fixing schema type based on timestamps schema options value
  • Loading branch information
ark23CIS committed Aug 25, 2024
commit 270b9f526bf153c50af67e85fc8f5efafe2223d8
27 changes: 27 additions & 0 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1604,3 +1604,30 @@ function gh13215() {
type SchemaType = InferSchemaType<typeof schema>;
expectType<User>({} as SchemaType);
}

function gh14825() {
const schemaDefinition = {
userName: { type: String, required: true }
} as const;
const schemaOptions = {
typeKey: 'type' as const,
timestamps: {
createdAt: 'date',
updatedAt: false
}
};

type RawDocType = InferRawDocType<
typeof schemaDefinition,
typeof schemaOptions
>;
type User = {
userName: string;
};

expectAssignable<User>({} as RawDocType);

const schema = new Schema(schemaDefinition, schemaOptions);
type SchemaType = InferSchemaType<typeof schema>;
expectAssignable<User>({} as SchemaType);
}
4 changes: 2 additions & 2 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ declare module 'mongoose' {
'createdAt' | 'updatedAt'
> as TimestampOptions[K] extends true
? K
: TimestampOptions[K] extends string
? TimestampOptions[K]
: TimestampOptions[K] extends `${infer TimestampValue}`
? TimestampValue
: never]: NativeDate;
} & T
: T
Expand Down