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

types: avoid automatically inferring lean result type when assigning to explicitly typed variable #14734

Merged
merged 1 commit into from
Jul 10, 2024

Conversation

vkarpov15
Copy link
Collaborator

Fix #14697

Summary

With the way that lean() is currently implemented, the following code unexpectedly compiles

export interface IUser {
  name: string;
  createdAt: Date;
  updatedAt: Date;
}
export interface IUserVirtuals {
  id: string;
}
type UserModelType = Model<IUser, {}, {}, IUserVirtuals>;
export type UserInstance = InstanceType<UserModelType>;


let user: UserInstance | null = null;
// Compiles even though `lean()` but `user` is hydrated doc
user = await UserModel.findOne().lean();

That's because TypeScript automatically infers the generic param to lean() if user has an explicit type.

In order to support this, I needed to change the default value of RawDocType param to Query<> to unknown. When RawDocType was set to DocType by default, that would break when DocType was set to be a hydrated document.

Examples

@vkarpov15 vkarpov15 added this to the 8.6 milestone Jul 9, 2024
@hasezoey hasezoey added the typescript Types or Types-test related issue / Pull Request label Jul 10, 2024
@vkarpov15 vkarpov15 changed the base branch from master to 8.6 July 10, 2024 15:47
@vkarpov15 vkarpov15 merged commit bc13f15 into 8.6 Jul 10, 2024
5 checks passed
@hasezoey hasezoey deleted the vkarpov15/gh-14697-2 branch July 10, 2024 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
2 participants