You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Although using the generic T has good intentions at including model properties, the union with any overrides type checking, making the type simply "any."
My solution
Note that not everything in a model's schema is used by a constructor (which is likely why "any" was union'ed with in the first place.) My solution to this is by creating a DeepPartial type to make all properties and their nested properties optional. Credit for DeepPartial
We will take a look and see if this approach works. any is intentional - you can pass any object to a model constructor. But we may be able to figure out some way to make autocomplete work better without breaking everyone's code.
Thanks that was an accident and also thanks for looking into it.
Update on the usage with my first post.
What I proposed was unfortunately a breaking approach with the way some people set up tests.
See the following:
In this case, Partial and DeepPartial will throw the following typescript error,
Argument of type 'Partial<IUser>' is not assignable to parameter of type 'Partial<{ _id: Partial<{ generationTime: number; equals: Partial<{}>; getTimestamp: Partial<{}>; toHexString: Partial<{}>; }>; createdAt: Partial<{ toString: Partial<{}>; ... 42 more ...; [Symbol.toPrimitive]: Partial<...>; }>; ... 71 more ...; validateSync: Partial<...>; }>'. Types of property '$locals' are incompatible. Type 'Record<string, unknown> | undefined' is not assignable to type 'Partial<{ [x: string]: Partial<{}>; }> | undefined'. Type 'Record<string, unknown>' is not assignable to type 'Partial<{ [x: string]: Partial<{}>; }>'
so everyone would have to go into their code and change document variables to be defined as DeepPartial, which obviously is very bad.
I figure we'll just pull top-level keys from T - that's better than having any since at least the key names will show up, if not necessarily the correct typings.
Problem:
When using the model constructor, the doc argument is typed with any. Shown here:
Source of the issue in index.d.ts (line 631):
Although using the generic T has good intentions at including model properties, the union with any overrides type checking, making the type simply "any."
My solution
Note that not everything in a model's schema is used by a constructor (which is likely why "any" was union'ed with in the first place.) My solution to this is by creating a DeepPartial type to make all properties and their nested properties optional. Credit for DeepPartial
Here is the diff that solved my problem:
This issue body was partially generated by patch-package.
Look at that beautiful type checking! 🎉
A better solution may exist because the time complexity of the DeepPartial isn't great using looping.
The text was updated successfully, but these errors were encountered: