From 0b78e822945c854b5fc97f2fe07e8160ae2c5b47 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 2 Nov 2022 12:32:17 -0400 Subject: [PATCH] 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'; + } } } }