Skip to content

Commit

Permalink
Pass DocType to statics, methods & query props in SchemaOptions to ge…
Browse files Browse the repository at this point in the history
…t better intellisense.
  • Loading branch information
mohammad0-0ahmad committed Mar 29, 2022
1 parent 1b00038 commit d499f2e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Schema, Document, SchemaDefinition, Model, Types, InferSchemaType, SchemaType, Query } from 'mongoose';
import { expectType, expectError, expectAssignable, expectNotAssignable } from 'tsd';
import { expectType, expectError, expectAssignable } from 'tsd';

enum Genre {
Action,
Expand Down Expand Up @@ -439,19 +439,19 @@ export function autoTypedSchema() {
}, {
statics: {
staticFn() {
expectAssignable<Model<any>>(this);
expectType<Model<M0_0aAutoTypedSchemaType['schema']>>(this);
return 'Returned from staticFn' as const;
}
},
methods: {
instanceFn() {
expectAssignable<Document>(this);
expectAssignable<Document<any, any, M0_0aAutoTypedSchemaType['schema']>>(this);
return 'Returned from DocumentInstanceFn' as const;
}
},
query: {
byUserName(userName) {
expectAssignable<Query<unknown, unknown>>(this);
expectAssignable<Query<unknown, M0_0aAutoTypedSchemaType['schema']>>(this);
return this.where({ userName });
}
}
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ declare module 'mongoose' {
/**
* Create a new schema
*/
constructor(definition?: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | DocType, options?: SchemaOptions<TPathTypeKey, TInstanceMethods, TQueryHelpers, TStaticMethods>);
constructor(definition?: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | DocType, options?: SchemaOptions<TPathTypeKey, DocType, TInstanceMethods, TQueryHelpers, TStaticMethods>);

/** Adds key path / schema type pairs to this schema. */
add(obj: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | Schema, prefix?: string): this;
Expand Down
8 changes: 4 additions & 4 deletions types/schemaoptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ declare module 'mongoose' {
type TypeKeyBaseType = string;

type DefaultTypeKey = 'type';
interface SchemaOptions<PathTypeKey extends TypeKeyBaseType = DefaultTypeKey, InstanceMethods = {}, QueryHelpers = {}, StaticMethods = {}> {
interface SchemaOptions<PathTypeKey extends TypeKeyBaseType = DefaultTypeKey, DocType = unknown, InstanceMethods = {}, QueryHelpers = {}, StaticMethods = {}> {
/**
* 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
Expand Down Expand Up @@ -192,16 +192,16 @@ declare module 'mongoose' {
/**
* Model Statics methods.
*/
statics?: Record<any, <T extends Model<any>>(this: T, ...args: any) => unknown> | StaticMethods,
statics?: Record<any, <T extends Model<DocType>>(this: T, ...args: any) => unknown> | StaticMethods,

/**
* Document instance methods.
*/
methods?: Record<any, <T extends Document>(this: T, ...args: any) => unknown> | InstanceMethods,
methods?: Record<any, <T extends Document<any, any, DocType>>(this: T, ...args: any) => unknown> | InstanceMethods,

/**
* Query helper functions
*/
query?: Record<any, <T extends QueryWithHelpers<unknown, unknown>>(this: T, ...args: any) => T> | QueryHelpers,
query?: Record<any, <T extends QueryWithHelpers<unknown, DocType>>(this: T, ...args: any) => T> | QueryHelpers,
}
}

0 comments on commit d499f2e

Please sign in to comment.