Skip to content

Commit

Permalink
Delete InferDocumentType helper & fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammad0-0ahmad committed Jul 19, 2022
1 parent 3e9ea71 commit f5983e6
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 18 deletions.
8 changes: 4 additions & 4 deletions test/types/virtuals.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Document, Model, Schema, model, InferSchemaType, InferDocumentType, FlatRecord } from 'mongoose';
import { Document, Model, Schema, model, InferSchemaType, FlatRecord, ObtainSchemaGeneric } from 'mongoose';
import { expectType } from 'tsd';

interface IPerson {
Expand Down Expand Up @@ -90,7 +90,7 @@ function gh11543() {
function autoTypedVirtuals() {
type AutoTypedSchemaType = InferSchemaType<typeof testSchema>;
type VirtualsType = { domain: string };
type InferredDocType = InferDocumentType<typeof testSchema>;
type InferredDocType = FlatRecord<AutoTypedSchemaType & ObtainSchemaGeneric<typeof testSchema, 'TVirtuals'>>;

const testSchema = new Schema({
email: {
Expand Down Expand Up @@ -118,5 +118,5 @@ function autoTypedVirtuals() {
const doc = new TestModel();
expectType<string>(doc.domain);

expectType<FlatRecord<AutoTypedSchemaType &VirtualsType >>({} as InferredDocType);
}
expectType<FlatRecord<AutoTypedSchemaType & VirtualsType >>({} as InferredDocType);
}
12 changes: 0 additions & 12 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,6 @@ declare module 'mongoose' {
*/
type InferSchemaType<SchemaType> = ObtainSchemaGeneric<SchemaType, 'DocType'>;

/**
* @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 = InferDocumentType<typeof userSchema>;
* // result
* type UserType = {userName?: string}
*/
type InferDocumentType<SchemaType> = FlatRecord<InferSchemaType<SchemaType> & ObtainSchemaGeneric<SchemaType, 'TVirtuals'>>;

/**
* @summary Obtains schema Generic type by using generic alias.
* @param {TSchema} TSchema A generic of schema type instance.
Expand Down
2 changes: 1 addition & 1 deletion types/utility.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ declare module 'mongoose' {
*/
type FlatRecord<T> = { [K in keyof T]: T[K] };

/**
/**
* @summary Checks if a type is "Record" or "any".
* @description It Helps to check if user has provided schema type "EnforcedDocType"
* @param {T} T A generic type to be checked.
Expand Down
2 changes: 1 addition & 1 deletion types/virtuals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ declare module 'mongoose' {
type SchemaOptionsVirtualsPropertyType<DocType = any, VirtualPaths = Record<any, unknown>, TInstanceMethods = {}> = {
[K in keyof VirtualPaths]: VirtualPathFunctions<IsItRecordAndNotAny<DocType> extends true ? DocType : any, VirtualPaths[K], TInstanceMethods>
};
}
}

0 comments on commit f5983e6

Please sign in to comment.