diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index fb06d26f4a9..6817f01306c 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -1,4 +1,4 @@ -import { Schema, Document, SchemaDefinition, Model, Types, InferSchemaType, SchemaType, Query, HydratedDocument } from 'mongoose'; +import { Schema, Document, SchemaDefinition, Model, Types, InferSchemaType, SchemaType, Query, HydratedDocument, InferDocType, FlatRecord } from 'mongoose'; import { expectType, expectError, expectAssignable } from 'tsd'; enum Genre { @@ -507,6 +507,10 @@ export function autoTypedSchema() { expectError({} as InferredSchemaType); + type InferredDocType = InferDocType; + + expectType>({} as InferredDocType); + return AutoTypedSchema; } diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index c7c7f1adf0f..9af50e34483 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -23,7 +23,19 @@ declare module 'mongoose' { * // result * type UserType = {userName?: string} */ - type InferSchemaType = ObtainSchemaGeneric ; + type InferSchemaType = ObtainSchemaGeneric; + + /** + * @summary Obtains document plain doc type from Schema instance. + * @description Exactly like {@link InferSchemaType}, but it add virtual paths. + * @param {SchemaType} SchemaType A generic of schema type instance. + * @example + * const userSchema = new Schema({userName:String}); + * type UserType = InferDocType; + * // result + * type UserType = {userName?: string} + */ + type InferDocType = FlatRecord & ObtainSchemaGeneric>; /** * @summary Obtains schema Generic type by using generic alias. diff --git a/types/utility.d.ts b/types/utility.d.ts index 5051bde4d08..a97510dca14 100644 --- a/types/utility.d.ts +++ b/types/utility.d.ts @@ -31,4 +31,12 @@ type IfEquals = (() => G extends T ? 1 : 0) extends (() => G extends U ? 1 : 0) ? Y : N; + +/** + * @summary Converts Unions to one record "object". + * @description It makes intellisense dialog box easier to read as a single object instead of showing that in multiple object unions. + * @param {T} T The type to be converted. + */ + type FlatRecord = { [K in keyof T]: T[K] }; + }