-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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] Query with dotted paths #12064
Comments
just noticed that #10180 is a similar Issue, should that one be closed in favor of this (or vise versa)? |
I'm really wary of doing this because I imagine it'll be horrifically slow and cause a lot of impossible to debug "Type instantiation is excessively deep and possibly infinite" type errors. I've tried adding the following to type PathsToStringProps<T> = T extends string
? []
: {
[K in Extract<keyof T, string>]: [K, ...PathsToStringProps<T[K]>];
}[Extract<keyof T, string>];
type Join<T extends string[], D extends string> = T extends []
? never
: T extends [infer F]
? F
: T extends [infer F, ...infer R]
? F extends string
? `${F}${D}${Join<Extract<R, string[]>, D>}`
: never
: string;
type _FilterQuery<T> = {
[P in keyof T]?: Condition<T[P]>;
} & RootQuerySelector<T>;
/**
* Filter query to select the documents that match the query
* @example
* ```js
* { age: { $gte: 30 } }
* ```
*/
type FilterQuery<T> = _FilterQuery<T> & Join<PathsToStringProps<T>, '.'>; And I'm getting the following:
Haven't been able to find a workaround. Any suggestions @hasezoey ? |
no i dont have any solutions, and yes it would cause may "too deep" errors, because that is basically what this approach is "meant" to do, the only ways i could see are:
i had just noted it here because it was now possible and mongoose already uses it in places at runtime but didnt have it in types |
I stumbled upon this myself a couple of days ago and came up with a solution (whose performance is questionable but perhaps a bit better): |
Hey, I came up with a solution, as seen above ^ |
I'll provide some screenshots and sources for my tests and open a PR in a sec |
Unfortunately @PCOffline 's solution doesn't work too well, I tried it for #14615 but unfortunately results in an infinite type instantiation error in automatic schema inference.
Realistically, I don't think we can support this unless TypeScript has some sort of way to limit the depth of the recursion or avoiding infinite recursion. |
Prerequisites
Issue
Currently Query's do not have dotted path suggestions, but would be possible with current typescript.
See This Stackoverflow Answer
TL;DR:
a simple example of what the stackoverflow answer has does (note: the types are directly copied from the answer)
Disclaimer: i am by no means a expert at complex typescript types, i also have no clue about the effect on types performance this would bring
The text was updated successfully, but these errors were encountered: