Skip to content

Commit 5dc2e90

Browse files
authored
Merge pull request #14829 from ark23CIS/ark23cis/gh-14825
fix: fixing schema type based on timestamps schema options value
2 parents 3169d92 + 270b9f5 commit 5dc2e90

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

test/types/schema.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,3 +1604,30 @@ function gh13215() {
16041604
type SchemaType = InferSchemaType<typeof schema>;
16051605
expectType<User>({} as SchemaType);
16061606
}
1607+
1608+
function gh14825() {
1609+
const schemaDefinition = {
1610+
userName: { type: String, required: true }
1611+
} as const;
1612+
const schemaOptions = {
1613+
typeKey: 'type' as const,
1614+
timestamps: {
1615+
createdAt: 'date',
1616+
updatedAt: false
1617+
}
1618+
};
1619+
1620+
type RawDocType = InferRawDocType<
1621+
typeof schemaDefinition,
1622+
typeof schemaOptions
1623+
>;
1624+
type User = {
1625+
userName: string;
1626+
};
1627+
1628+
expectAssignable<User>({} as RawDocType);
1629+
1630+
const schema = new Schema(schemaDefinition, schemaOptions);
1631+
type SchemaType = InferSchemaType<typeof schema>;
1632+
expectAssignable<User>({} as SchemaType);
1633+
}

types/inferschematype.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ declare module 'mongoose' {
8787
'createdAt' | 'updatedAt'
8888
> as TimestampOptions[K] extends true
8989
? K
90-
: TimestampOptions[K] extends string
91-
? TimestampOptions[K]
90+
: TimestampOptions[K] extends `${infer TimestampValue}`
91+
? TimestampValue
9292
: never]: NativeDate;
9393
} & T
9494
: T

0 commit comments

Comments
 (0)