Skip to content

Commit

Permalink
fix: inferSchemaType, inferRawType updates to infer based on provided…
Browse files Browse the repository at this point in the history
… schema options as well
  • Loading branch information
ark23CIS committed Aug 1, 2024
1 parent 986f5eb commit c384eb0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
29 changes: 29 additions & 0 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1575,3 +1575,32 @@ function gh14748() {
const subdoc3 = schema.path<Schema.Types.Subdocument<{ name: string }>>('singleNested').cast({ name: 'bar' });
expectAssignable<{ name: string }>(subdoc3);
}

function gh13215() {
const schemaDefinition = {
userName: { type: String, required: true }
} as const;
const schemaOptions = {
typeKey: 'type',
timestamps: {
createdAt: 'date',
updatedAt: false
}
} as const;

type RawDocType = InferRawDocType<
typeof schemaDefinition,
typeof schemaOptions
>;
type User = {
userName: string;
} & {
date: Date;
};

expectType<User>({} as RawDocType);

const schema = new Schema(schemaDefinition, schemaOptions);
type SchemaType = InferSchemaType<typeof schema>;
expectType<User>({} as SchemaType);
}
4 changes: 2 additions & 2 deletions types/inferrawdoctype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ declare module 'mongoose' {
export type InferRawDocType<
DocDefinition,
TSchemaOptions extends Record<any, any> = DefaultSchemaOptions
> = {
> = ApplySchemaOptions<{
[
K in keyof (RequiredPaths<DocDefinition, TSchemaOptions['typeKey']> &
OptionalPaths<DocDefinition, TSchemaOptions['typeKey']>)
]: ObtainRawDocumentPathType<DocDefinition[K], TSchemaOptions['typeKey']>;
};
}, TSchemaOptions>;

/**
* @summary Obtains schema Path type.
Expand Down
21 changes: 16 additions & 5 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,25 @@ declare module 'mongoose' {

type ApplySchemaOptions<T, O = DefaultSchemaOptions> = ResolveTimestamps<T, O>;

type ResolveTimestamps<T, O> = O extends { timestamps: true }
type ResolveTimestamps<T, O> = O extends { methods: any } | { statics: any } | { virtuals: any } | { timestamps?: false } ? T
// For some reason, TypeScript sets all the document properties to unknown
// if we use methods, statics, or virtuals. So avoid inferring timestamps
// if any of these are set for now. See gh-12807
? O extends { methods: any } | { statics: any } | { virtuals: any }
? T
: { createdAt: NativeDate; updatedAt: NativeDate; } & T
: T;
: O extends { timestamps: infer TimestampOptions } ? TimestampOptions extends true
? { createdAt: NativeDate; updatedAt: NativeDate; } & T
: TimestampOptions extends SchemaTimestampsConfig
? {
-readonly [K in keyof Pick<
TimestampOptions,
'createdAt' | 'updatedAt'
> as TimestampOptions[K] extends true
? K
: TimestampOptions[K] extends string
? TimestampOptions[K]
: never]: NativeDate;
} & T
: T
: T;
}

type IsPathDefaultUndefined<PathType> = PathType extends { default: undefined } ?
Expand Down

0 comments on commit c384eb0

Please sign in to comment.