From efb11eac085f057a8fd3ed67fa6b5ba12696ea7e Mon Sep 17 00:00:00 2001 From: Mohammad Date: Thu, 21 Jul 2022 13:30:59 +0200 Subject: [PATCH] Replace Schema path generic TPathKey with TSchemaOptions to cover the entire schema optins one generic. --- test/types/schema.test.ts | 31 ++++++++++++++++--------------- types/index.d.ts | 6 +++--- types/inferschematype.d.ts | 17 ++++++++++------- types/schemaoptions.d.ts | 9 +++++++-- 4 files changed, 36 insertions(+), 27 deletions(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 8a0dd423801..1a74fccf027 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -9,7 +9,8 @@ import { SchemaType, Query, HydratedDocument, - SchemaOptions + SchemaOptions, + ObtainSchemaGeneric } from 'mongoose'; import { expectType, expectError, expectAssignable } from 'tsd'; @@ -599,20 +600,6 @@ function gh11997() { userSchema.index({ name: 1 }, { weights: { name: 1 } }); } -function gh12003() { - const baseSchemaOptions: SchemaOptions = { - versionKey: false - }; - - const BaseSchema = new Schema({ - name: String - }, baseSchemaOptions); - - type BaseSchemaType = InferSchemaType; - - expectType<{ name?: string }>({} as BaseSchemaType); -} - function gh11987() { interface IUser { name: string; @@ -702,3 +689,17 @@ function gh12030() { }>({} as InferSchemaType); } + +function gh12122() { + const Test1 = new Schema({ test: String }, { typeKey: 'customTypeKey' }); + expectType<{ + typeKey: 'customTypeKey'; + id: true; + }>({} as ObtainSchemaGeneric); + + const Test2 = new Schema({ test: String }, {}); + expectType<{ + typeKey: 'type'; + id: true; + }>({} as ObtainSchemaGeneric); +} diff --git a/types/index.d.ts b/types/index.d.ts index 24c358f9a95..17d9e9e5a50 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -159,13 +159,13 @@ declare module 'mongoose' { export class Schema, TInstanceMethods = {}, TQueryHelpers = {}, TVirtuals = {}, TStaticMethods = {}, - TPathTypeKey extends TypeKeyBaseType = DefaultTypeKey, - DocType extends ObtainDocumentType = ObtainDocumentType> + TSchemaOptions extends ResolveSchemaOptions = DefaultSchemaOptions, + DocType extends ObtainDocumentType = ObtainDocumentType> extends events.EventEmitter { /** * Create a new schema */ - constructor(definition?: SchemaDefinition> | DocType, options?: SchemaOptions, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals>); + constructor(definition?: SchemaDefinition> | DocType, options?: SchemaOptions, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals> | TSchemaOptions); /** Adds key path / schema type pairs to this schema. */ add(obj: SchemaDefinition> | Schema, prefix?: string): this; diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index 5041f830477..6462e3de160 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -23,10 +23,10 @@ declare module 'mongoose' { * @param {EnforcedDocType} EnforcedDocType A generic type enforced by user "provided before schema constructor". * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition". */ - type ObtainDocumentType = + type ObtainDocumentType = DefaultSchemaOptions> = IsItRecordAndNotAny extends true ? EnforcedDocType : { - [K in keyof (RequiredPaths & - OptionalPaths)]: ObtainDocumentPathType; + [K in keyof (RequiredPaths & + OptionalPaths)]: ObtainDocumentPathType; }; /** @@ -45,8 +45,8 @@ declare module 'mongoose' { * @param {TSchema} TSchema A generic of schema type instance. * @param {alias} alias Targeted generic alias. */ - type ObtainSchemaGeneric = - TSchema extends Schema + type ObtainSchemaGeneric = + TSchema extends Schema ? { EnforcedDocType: EnforcedDocType; M: M; @@ -54,12 +54,15 @@ declare module 'mongoose' { TQueryHelpers: TQueryHelpers; TVirtuals: TVirtuals; TStaticMethods: TStaticMethods; - TPathTypeKey: TPathTypeKey; + TSchemaOptions: TSchemaOptions; DocType: DocType; }[alias] : unknown; + + type ResolveSchemaOptions = Omit, 'statics' | 'methods' | 'query' | 'virtuals'>; } + /** * @summary Checks if a document path is required or optional. * @param {P} P Document path. @@ -168,5 +171,5 @@ type ResolvePathType extends true ? any: IfEquals extends true ? any: PathValueType extends typeof SchemaType ? PathValueType['prototype'] : - PathValueType extends Record ? ObtainDocumentType : + PathValueType extends Record ? ObtainDocumentType : unknown; diff --git a/types/schemaoptions.d.ts b/types/schemaoptions.d.ts index 1a87a8ccf9f..5fc9b64997b 100644 --- a/types/schemaoptions.d.ts +++ b/types/schemaoptions.d.ts @@ -10,7 +10,7 @@ declare module 'mongoose' { type TypeKeyBaseType = string; type DefaultTypeKey = 'type'; - interface SchemaOptions { + interface SchemaOptions { /** * By default, Mongoose's init() function creates all the indexes defined in your model's schema by * calling Model.createIndexes() after you successfully connect to MongoDB. If you want to disable @@ -139,7 +139,7 @@ declare module 'mongoose' { * type declaration. However, for applications like geoJSON, the 'type' property is important. If you want to * control which key mongoose uses to find type declarations, set the 'typeKey' schema option. */ - typeKey?: PathTypeKey; + typeKey?: TypeKeyBaseType; /** * By default, documents are automatically validated before they are saved to the database. This is to @@ -214,4 +214,9 @@ declare module 'mongoose' { */ virtuals?: SchemaOptionsVirtualsPropertyType, } + + interface DefaultSchemaOptions { + typeKey: 'type' + id: true + } }