Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
WIP alternative approach for #13523: create separate generic for sche…
…ma definition
  • Loading branch information
vkarpov15 committed Sep 1, 2023
commit 6b125a082f2dd1ddd870ce4a23fca862f2d1c05e
4 changes: 3 additions & 1 deletion test/types/querycursor.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Schema, model, Model, Types } from 'mongoose';
import { expectType } from 'tsd';

const schema = new Schema({ name: { type: 'String' } });
const schema = new Schema({ name: { type: 'String' as const } });

const Test = model('Test', schema);

Expand All @@ -20,3 +20,5 @@ Test.find().cursor().
expectType<number>(i);
}).
then(() => console.log('Done!'));

expectType<{ name: { type: 'String' } }>(schema.obj);
4 changes: 2 additions & 2 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ function gh12869() {

const dbExample = new Schema(
{
active: { type: String, enum: ['foo', 'bar'], required: true }
active: { type: String, enum: ['foo', 'bar'] as ['foo', 'bar'], required: true }
}
);

Expand All @@ -1031,7 +1031,7 @@ function gh12882() {
const arrNum = new Schema({
fooArray: {
type: [{
type: 'Number',
type: 'Number' as const,
required: true
}],
required: true
Expand Down
29 changes: 15 additions & 14 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ declare module 'mongoose' {
collection?: string,
options?: CompileModelOptions
): Model<
InferSchemaType<TSchema>,
ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>,
ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
ObtainSchemaGeneric<TSchema, 'TVirtuals'>,
HydratedDocument<
InferSchemaType<TSchema>,
ObtainSchemaGeneric<TSchema, 'TVirtuals'> & ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>
>,
TSchema
InferSchemaType<TSchema>,
ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>,
ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
ObtainSchemaGeneric<TSchema, 'TVirtuals'>,
HydratedDocument<
InferSchemaType<TSchema>,
ObtainSchemaGeneric<TSchema, 'TVirtuals'> & ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>
>,
TSchema
> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;

export function model<T>(name: string, schema?: Schema<T, any, any> | Schema<T & Document, any, any>, collection?: string, options?: CompileModelOptions): Model<T>;
Expand Down Expand Up @@ -223,11 +223,12 @@ declare module 'mongoose' {
TVirtuals = {},
TStaticMethods = {},
TSchemaOptions = DefaultSchemaOptions,
TSchemaDefinition = any,
DocType extends ApplySchemaOptions<
ObtainDocumentType<DocType, EnforcedDocType, ResolveSchemaOptions<TSchemaOptions>>,
ObtainDocumentType<TSchemaDefinition, EnforcedDocType, ResolveSchemaOptions<TSchemaOptions>>,
ResolveSchemaOptions<TSchemaOptions>
> = ApplySchemaOptions<
ObtainDocumentType<any, EnforcedDocType, ResolveSchemaOptions<TSchemaOptions>>,
ObtainDocumentType<TSchemaDefinition, EnforcedDocType, ResolveSchemaOptions<TSchemaOptions>>,
ResolveSchemaOptions<TSchemaOptions>
>,
THydratedDocumentType = HydratedDocument<DocType, TVirtuals & TInstanceMethods>
Expand All @@ -236,7 +237,7 @@ declare module 'mongoose' {
/**
* Create a new schema
*/
constructor(definition?: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | DocType, options?: SchemaOptions<DocType, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals, THydratedDocumentType> | ResolveSchemaOptions<TSchemaOptions>);
constructor(definition?: TSchemaDefinition, options?: SchemaOptions<DocType, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals, THydratedDocumentType> | ResolveSchemaOptions<TSchemaOptions>);

/** Adds key path / schema type pairs to this schema. */
add(obj: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | Schema, prefix?: string): this;
Expand Down Expand Up @@ -300,7 +301,7 @@ declare module 'mongoose' {
methods: { [F in keyof TInstanceMethods]: TInstanceMethods[F] } & AnyObject;

/** The original object passed to the schema constructor */
obj: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>>;
obj: TSchemaDefinition;

/** Gets/sets schema paths. */
path<ResultType extends SchemaType = SchemaType<any, THydratedDocumentType>>(path: string): ResultType;
Expand Down
5 changes: 3 additions & 2 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ declare module 'mongoose' {
* @param {TSchema} TSchema A generic of schema type instance.
* @param {alias} alias Targeted generic alias.
*/
type ObtainSchemaGeneric<TSchema, alias extends 'EnforcedDocType' | 'M' | 'TInstanceMethods' | 'TQueryHelpers' | 'TVirtuals' | 'TStaticMethods' | 'TSchemaOptions' | 'DocType'> =
TSchema extends Schema<infer EnforcedDocType, infer M, infer TInstanceMethods, infer TQueryHelpers, infer TVirtuals, infer TStaticMethods, infer TSchemaOptions, infer DocType>
type ObtainSchemaGeneric<TSchema, alias extends 'EnforcedDocType' | 'M' | 'TInstanceMethods' | 'TQueryHelpers' | 'TVirtuals' | 'TStaticMethods' | 'TSchemaOptions' | 'TSchemaDefinition' | 'DocType'> =
TSchema extends Schema<infer EnforcedDocType, infer M, infer TInstanceMethods, infer TQueryHelpers, infer TVirtuals, infer TStaticMethods, infer TSchemaOptions, infer TSchemaDefinition, infer DocType>
? {
EnforcedDocType: EnforcedDocType;
M: M;
Expand All @@ -56,6 +56,7 @@ declare module 'mongoose' {
TVirtuals: TVirtuals;
TStaticMethods: TStaticMethods;
TSchemaOptions: TSchemaOptions;
TSchemaDefinition: TSchemaDefinition;
DocType: DocType;
}[alias]
: unknown;
Expand Down