diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index b0e148afce5..88a9a40d148 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -873,6 +873,29 @@ function gh12431() { expectType<{ testDate?: Date, testDecimal?: Types.Decimal128 }>({} as Example); } +async function gh12593() { + const testSchema = new Schema({ x: { type: Schema.Types.UUID } }); + + type Example = InferSchemaType; + expectType<{ x?: Buffer }>({} as Example); + + const Test = model('Test', testSchema); + + const doc = await Test.findOne({ x: '4709e6d9-61fd-435e-b594-d748eb196d8f' }).orFail(); + expectType(doc.x); + + const doc2 = new Test({ x: '4709e6d9-61fd-435e-b594-d748eb196d8f' }); + expectType(doc2.x); + + const doc3 = await Test.findOne({}).orFail().lean(); + expectType(doc3.x); + + const arrSchema = new Schema({ arr: [{ type: Schema.Types.UUID }] }); + + type ExampleArr = InferSchemaType; + expectType<{ arr: Buffer[] }>({} as ExampleArr); +} + function gh12562() { const emailRegExp = /@/; const userSchema = new Schema( diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index 27090b89eb4..64eb6fdc95f 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -177,11 +177,13 @@ type ResolvePathType extends true ? Types.Decimal128 : IfEquals extends true ? Types.Decimal128 : - PathValueType extends MapConstructor ? Map> : - PathValueType extends ArrayConstructor ? any[] : - PathValueType extends typeof Schema.Types.Mixed ? any: - IfEquals extends true ? any: - IfEquals extends true ? any: - PathValueType extends typeof SchemaType ? PathValueType['prototype'] : - PathValueType extends Record ? ObtainDocumentType : - unknown; + PathValueType extends 'uuid' | 'UUID' | typeof Schema.Types.UUID ? Buffer : + IfEquals extends true ? Buffer : + PathValueType extends MapConstructor ? Map> : + PathValueType extends ArrayConstructor ? any[] : + PathValueType extends typeof Schema.Types.Mixed ? any: + IfEquals extends true ? any: + IfEquals extends true ? any: + PathValueType extends typeof SchemaType ? PathValueType['prototype'] : + PathValueType extends Record ? ObtainDocumentType : + unknown; diff --git a/types/schematypes.d.ts b/types/schematypes.d.ts index 093651d1408..0293f9dc05e 100644 --- a/types/schematypes.d.ts +++ b/types/schematypes.d.ts @@ -422,6 +422,11 @@ declare module 'mongoose' { /** Adds an uppercase [setter](http://mongoosejs.com/docs/api.html#schematype_SchemaType-set). */ uppercase(shouldApply?: boolean): this; } + + class UUID extends SchemaType { + /** This schema type's name, to defend against minifiers that mangle function names. */ + static schemaName: 'UUID'; + } } } }