Skip to content

Commit c384eb0

Browse files
committed
fix: inferSchemaType, inferRawType updates to infer based on provided schema options as well
1 parent 986f5eb commit c384eb0

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

test/types/schema.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,3 +1575,32 @@ function gh14748() {
15751575
const subdoc3 = schema.path<Schema.Types.Subdocument<{ name: string }>>('singleNested').cast({ name: 'bar' });
15761576
expectAssignable<{ name: string }>(subdoc3);
15771577
}
1578+
1579+
function gh13215() {
1580+
const schemaDefinition = {
1581+
userName: { type: String, required: true }
1582+
} as const;
1583+
const schemaOptions = {
1584+
typeKey: 'type',
1585+
timestamps: {
1586+
createdAt: 'date',
1587+
updatedAt: false
1588+
}
1589+
} as const;
1590+
1591+
type RawDocType = InferRawDocType<
1592+
typeof schemaDefinition,
1593+
typeof schemaOptions
1594+
>;
1595+
type User = {
1596+
userName: string;
1597+
} & {
1598+
date: Date;
1599+
};
1600+
1601+
expectType<User>({} as RawDocType);
1602+
1603+
const schema = new Schema(schemaDefinition, schemaOptions);
1604+
type SchemaType = InferSchemaType<typeof schema>;
1605+
expectType<User>({} as SchemaType);
1606+
}

types/inferrawdoctype.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ declare module 'mongoose' {
1010
export type InferRawDocType<
1111
DocDefinition,
1212
TSchemaOptions extends Record<any, any> = DefaultSchemaOptions
13-
> = {
13+
> = ApplySchemaOptions<{
1414
[
1515
K in keyof (RequiredPaths<DocDefinition, TSchemaOptions['typeKey']> &
1616
OptionalPaths<DocDefinition, TSchemaOptions['typeKey']>)
1717
]: ObtainRawDocumentPathType<DocDefinition[K], TSchemaOptions['typeKey']>;
18-
};
18+
}, TSchemaOptions>;
1919

2020
/**
2121
* @summary Obtains schema Path type.

types/inferschematype.d.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,25 @@ declare module 'mongoose' {
7474

7575
type ApplySchemaOptions<T, O = DefaultSchemaOptions> = ResolveTimestamps<T, O>;
7676

77-
type ResolveTimestamps<T, O> = O extends { timestamps: true }
77+
type ResolveTimestamps<T, O> = O extends { methods: any } | { statics: any } | { virtuals: any } | { timestamps?: false } ? T
7878
// For some reason, TypeScript sets all the document properties to unknown
7979
// if we use methods, statics, or virtuals. So avoid inferring timestamps
8080
// if any of these are set for now. See gh-12807
81-
? O extends { methods: any } | { statics: any } | { virtuals: any }
82-
? T
83-
: { createdAt: NativeDate; updatedAt: NativeDate; } & T
84-
: T;
81+
: O extends { timestamps: infer TimestampOptions } ? TimestampOptions extends true
82+
? { createdAt: NativeDate; updatedAt: NativeDate; } & T
83+
: TimestampOptions extends SchemaTimestampsConfig
84+
? {
85+
-readonly [K in keyof Pick<
86+
TimestampOptions,
87+
'createdAt' | 'updatedAt'
88+
> as TimestampOptions[K] extends true
89+
? K
90+
: TimestampOptions[K] extends string
91+
? TimestampOptions[K]
92+
: never]: NativeDate;
93+
} & T
94+
: T
95+
: T;
8596
}
8697

8798
type IsPathDefaultUndefined<PathType> = PathType extends { default: undefined } ?

0 commit comments

Comments
 (0)