Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix types resolved from InferSchemaType #12450

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 32 additions & 0 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,38 @@ function gh12030() {

}

function gh12450() {
const ObjectIdSchema = new Schema({
user: { type: Schema.Types.ObjectId }
});

expectType<{
user?: Types.ObjectId;
}>({} as InferSchemaType<typeof ObjectIdSchema>);

const Schema2 = new Schema({
createdAt: { type: Date, required: true },
decimalValue: { type: Schema.Types.Decimal128, required: true }
});

expectType<{ createdAt: Date, decimalValue: Types.Decimal128 }>({} as InferSchemaType<typeof Schema2>);

const Schema3 = new Schema({
createdAt: { type: Date, required: true },
decimalValue: { type: Schema.Types.Decimal128 }
});

expectType<{ createdAt: Date, decimalValue?: Types.Decimal128 }>({} as InferSchemaType<typeof Schema3>);

const Schema4 = new Schema({
createdAt: { type: Date },
decimalValue: { type: Schema.Types.Decimal128 }
});

expectType<{ createdAt?: Date, decimalValue?: Types.Decimal128 }>({} as InferSchemaType<typeof Schema4>);

}

function pluginOptions() {
interface SomePluginOptions {
option1?: string;
Expand Down
10 changes: 5 additions & 5 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,11 @@ declare module 'mongoose' {
virtualpath<T = HydratedDocument<DocType, TInstanceMethods>>(name: string): VirtualType<T> | null;
}

export type NumberSchemaDefinition = typeof Number | 'number' | 'Number' | typeof Schema.Types.Number;
export type StringSchemaDefinition = typeof String | 'string' | 'String' | typeof Schema.Types.String;
export type BooleanSchemaDefinition = typeof Boolean | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean;
export type DateSchemaDefinition = typeof NativeDate | 'date' | 'Date' | typeof Schema.Types.Date;
export type ObjectIdSchemaDefinition = 'ObjectId' | 'ObjectID' | typeof Schema.Types.ObjectId;
export type NumberSchemaDefinition = typeof Number | 'number' | 'Number' | typeof Schema.Types.Number | Number;
export type StringSchemaDefinition = typeof String | 'string' | 'String' | typeof Schema.Types.String | String;
export type BooleanSchemaDefinition = typeof Boolean | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean | Boolean;
export type DateSchemaDefinition = typeof NativeDate | 'date' | 'Date' | typeof Schema.Types.Date | Date;
export type ObjectIdSchemaDefinition = 'ObjectId' | 'ObjectID' | typeof Schema.Types.ObjectId | Schema.Types.ObjectId;

export type SchemaDefinitionWithBuiltInClass<T> = T extends number
? NumberSchemaDefinition
Expand Down
25 changes: 17 additions & 8 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ declare module 'mongoose' {
* // result
* type UserType = {userName?: string}
*/
type InferSchemaType<SchemaType> = ObtainSchemaGeneric<SchemaType, 'DocType'>;
type InferSchemaType<SchemaType> = ObtainDocType<SchemaType>; // "ObtainDocType" is used over "ObtainSchemaGeneric", because "ObtainSchemaGeneric" would return a modified result;

/**
* @summary Obtains the "DocType" generic of a Schema, has to be used because with "ObtainSchemaGeneric" it would return a modified type
* @param {TSchema} TSchema A generic of schema type instance
*/
type ObtainDocType<TSchema> =
TSchema extends Schema<any, any, any, any, any, any, any, infer DocType>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't do this. For some reason, not inferring all the generics (there's one in particular that is important that I forget) breaks stuff. There's a little more info on this PR: #12352 . I don't know why, TypeScript is filled with delightful little surprises like that.

? DocType
: unknown;

/**
* @summary Obtains schema Generic type by using generic alias.
Expand Down Expand Up @@ -155,13 +164,13 @@ type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends (
type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}, TypeKey extends TypeKeyBaseType = DefaultTypeKey> =
PathValueType extends Schema ? InferSchemaType<PathValueType> :
PathValueType extends (infer Item)[] ? IfEquals<Item, never, any[], Item extends Schema ? Types.DocumentArray<ResolvePathType<Item>> : ResolvePathType<Item>[]> :
PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']> :
PathValueType extends NumberSchemaDefinition ? Options['enum'] extends ReadonlyArray<any> ? Options['enum'][number] : number :
PathValueType extends DateSchemaDefinition ? Date :
PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer :
PathValueType extends BooleanSchemaDefinition ? boolean :
PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId :
PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 :
PathValueType extends NumberSchemaDefinition ? Options['enum'] extends ReadonlyArray<any> ? Options['enum'][number] : number :
PathValueType extends DateSchemaDefinition ? Date :
PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer :
PathValueType extends BooleanSchemaDefinition ? boolean :
PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId :
PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 | Schema.Types.Decimal128 ? Types.Decimal128 :
PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']> :
PathValueType extends MapConstructor ? Map<string, ResolvePathType<Options['of']>> :
PathValueType extends ArrayConstructor ? any[] :
PathValueType extends typeof Schema.Types.Mixed ? any:
Expand Down