I would like to write a function which can operate over any object set so long as the base object type contains a specific property. How can I do this?
What I’ve tried:
function filterMyProperty<
OT extends ObjectTypeDefinition & {
_DefinitionMetadata?: {
properties: {
myProperty: PropertyDef<"boolean", "nullable" | "non-nullable", "single">
}
}
}
>(
objectSet: ObjectSet<OT>,
myPropertyValue: boolean
) {
const whereClause: WhereClause<OT> = {
myProperty: {
$eq: myPropertyValue
}
};
return objectSet.where(whereClause);
}
Unfortunately the where clause definition has a TypeScript error on myProperty: which reads Object literal may only specify known properties, and ‘myProperty’ does not exist in type ‘OrWhereClause<OT> | AndWhereClause<OT> | NotWhereClause<OT>’. ts(2353)
Below that, the VSCode mouse-over has (property) myProperty?: FilterFor<CompileTimeMetadata<OT>[“properties”][“myProperty”]> | undefined which should evaluate to a boolean filter, which I have provided.
Interestingly, the following is fine:
function filterMyProperty<
OT extends ObjectTypeDefinition & {
_DefinitionMetadata?: {
properties: {
myProperty: PropertyDef<"boolean", "nullable" | "non-nullable", "single">
}
}
}
>(
objectType: OT,
myPropertyValue: boolean
) {
return client(objectType).where({
myProperty: {
$eq: myPropertyValue
},
somePropertyThatDoesNotExistOnAnyObject: {}
});
}
This ^ doesn’t show any TypeScript errors, though I think it should. Mousing over .where reveals any, which is why this doesn’t error.