I’m using the @directus/sdk and I’d like to filter on date ranges. For example, I’d like to filter a collection where a date field is in 2024. I figured the most direct solution would be something like this:
readItems('mycollection', {
filter: {
some_date: {
_gte: '2024-01-01',
_lte: '2024-12-31',
},
},
})
Unfortunately, this throws the error:
Object literal may only specify known properties, and ‘_gte’ does not exist in type ‘MapFilterOperators<{ _eq: Date; _neq: Date; _gt: never; _gte: never; _lt: never; _lte: never; _in: Date; _nin: Date; _between: never; _nbetween: never; _contains: never; _ncontains: never; _icontains: never; … 15 more …; _nintersects_bbox: Date; }>’.
I dug through the docs and I’ve searched the community but I can’t seem to figure out what the preferred solution is for doing this.
Any ideas?
Thanks!
Hello @Chromag
Try this
readItems('mycollection', {
filter: {
"year(some_date)": {
"_eq": "2024"
},
},
})
This is indeed a bug in the TS types of the SDK. Try one to solve as there’s so many permutations of field names with these functions.. I’ll fly in an issue on GitHub so we don’t lose track of this 
@rijkvanzanten I’ll go with the _gte / _lte solution for now so I don’t have to add any hackiness to my collection type. Thanks for following up! Glad to hear it’ll be fixed at some point but at least I have a workaround for now.
Hi @ahmad_quvor
Thanks for the suggestion! That was one of the first things I tried. Unfortunately, this throws a Typescript error because it’s expecting a “known” property.
Object literal may only specify known properties, and ‘‘year(some_date)’’ does not exist in type ‘QueryFilter<DirectusSchema, DirectusProject>’.
It did indeed work. I tried a hacky workaround and just added year(some_date) as a property on my DirectusProject type (the type that gets returned for this query).
export interface DirectusProject {
id?: string;
...
// query filter-related
// eslint-disable-next-line @typescript-eslint/no-explicit-any
'year(some_date)': any;
}
I don’t particularly want to use any in this scenario so I may do some digging to figure out the correct type.
Including the filter prop in the collection type just seems like a pretty hacky solution. Wondering if this is the best-practice solution for this kind of query using the SDK and Typescript.
EDIT: I can also get it to work like this without having to modify the collection type. This also requires that I cast the filter as any to get around the typescript errors (which also seems hacky):
const projects = await directusClient.request(
readItems('mycollection', {
filter: {
some_date: {
_gte: `${year}-01-01`,
_lte: `${year}-12-31`,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
},
})
);
Thanks for your assistance.