Skip to content

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
@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
Development

Successfully merging this pull request may close these issues.

TypeScript does not return an error when assigning the result of a lean query to a variable of type InstanceType<Model<MyModel>>
2 participants