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: support schema type inference based on schema options timestamps as well #14773

Merged
merged 1 commit into from
Aug 7, 2024
Merged
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
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