I have a type that defines an object wh's keys are the nested keys of another type and who's values are ether the same type as the original object, or a set object type.
export type SQLMatchers<Schema extends object> = {
[Key in NestedKeyOf<Schema>]?: NestedValue<Schema, Key> extends Date
? Date | SQLMatcherOperators
: NestedValue<Schema, Key> | SQLMatcherOperators;
};
This type works great for what I want to do, my issue comes with the date, The original object values can ONLY be primitives or a Date object, which, for the primitives works fantastic since ther you set a propert directly to a primitive value, or if you set it to an object it WILL be the SQLMatcherOperators object.
My issue comes from date, since the Date constructor does resolve to an object, Ts does not diferenciate and spills it's properties into my SQLMatcherOperators object.
to resolve this issue (at least theoretically) I would replace the union between Date and SQLMatcherOperators to be something like a OR operator, but so far I've found nothing of the sort.
I'll use this example:
const ExampleSchema = new SQLSchema<History>(
{
// ... other properties
details:{
// ... other properties
newValue: String,
test: Date,
},
}
);
So given that schema, the matchers can be defined as follows:
const result = await ExampleModel.findOne({
"details.newValue": "foo",
"details.test": new Date(),
});
where:
(property) "details.newValue"?: string | SQLMatcherOperators
and
(property) "details.test"?: Date | SQLMatcherOperators
All fine so far, the thing is once you use the matchers object, in the case of string it's image[1], perfect, but in case of date it's image[2]
I've tried setting the type to an "invisible date" where I set the object as date, but i omit it's keys, but that did not work bc TS decided that is equal to any type.
