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: fixing schema type based on timestamps schema options value #14829

Merged
merged 1 commit into from
Aug 26, 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
27 changes: 27 additions & 0 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1604,3 +1604,30 @@ function gh13215() {
type SchemaType = InferSchemaType<typeof schema>;
expectType<User>({} as SchemaType);
}

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

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

expectAssignable<User>({} as RawDocType);

const schema = new Schema(schemaDefinition, schemaOptions);
type SchemaType = InferSchemaType<typeof schema>;
expectAssignable<User>({} as SchemaType);
}
4 changes: 2 additions & 2 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ declare module 'mongoose' {
'createdAt' | 'updatedAt'
> as TimestampOptions[K] extends true
? K
: TimestampOptions[K] extends string
? TimestampOptions[K]
: TimestampOptions[K] extends `${infer TimestampValue}`
? TimestampValue
: never]: NativeDate;
} & T
: T
Expand Down