From 0b78e822945c854b5fc97f2fe07e8160ae2c5b47 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 2 Nov 2022 12:32:17 -0400 Subject: [PATCH 1/2] fix(types): add UUID to types Fix #12593 Re: #12268 Re: #3208 --- test/types/schema.test.ts | 7 +++++++ types/inferschematype.d.ts | 18 ++++++++++-------- types/schematypes.d.ts | 5 +++++ 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 86e4d2668a4..f5a938e2fb8 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -870,3 +870,10 @@ function gh12431() { type Example = InferSchemaType; expectType<{ testDate?: Date, testDecimal?: Types.Decimal128 }>({} as Example); } + +function gh12593() { + const testSchema = new Schema({ x: { type: Schema.Types.UUID } }); + + type Example = InferSchemaType; + expectType<{ x?: Buffer }>({} as Example); +} diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index a0406388f00..4051b861be7 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -171,11 +171,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 899660d9fe6..7267939764c 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'; + } } } } From 243e4d4f14685e88c906dd26476c00e885bd278d Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 2 Nov 2022 16:59:17 -0400 Subject: [PATCH 2/2] test: add extra typescript test coverage for UUID schematype --- test/types/schema.test.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index f5a938e2fb8..84e963c96f3 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -871,9 +871,25 @@ function gh12431() { expectType<{ testDate?: Date, testDecimal?: Types.Decimal128 }>({} as Example); } -function gh12593() { +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); }