Constantly getting this error and cant get pass by
const topics = useQuery(api.titles.collectTitles);
const filteredTopics = useMemo(
() =>
Object.entries(topics).reduce((acc, [key, topic]) => {
if (
fields.categories.length > 0 &&
(!topic.category || !fields.categories.includes(topic.category))
) {
return acc;
}
if (
fields.questionTypes.length > 0 &&
!fields.questionTypes.includes(topic.questionType as string)
) {
return acc;
}
if (
fields.search &&
!topic.name.toLowerCase().includes(fields.search.toLowerCase()) &&
!topic.questionDesc
?.toString()
.toLowerCase()
.includes(fields.search.toLowerCase())
) {
return acc;
}
acc?.push(topic);
return acc;
}, {} as typeof topics),
[fields, topics]
);
Type 'undefined' is not assignable to type '{}'.ts(2769)
This error is showing up here when the topics is declared Object.entries(topics).reduce5 Replies
It looks like this is TypeScript helping out appropriately: while the query is loading,
topics
will indeed be undefined
!
and Object.entries(undefined)
throws an error
You could use a conditional if (topics === undefined) return <Loading />;
or use a default value like const topics = useQuery(api.titles.collectTitles) || []
@ballingt is
??
more idiomatic these days, or am I off?yeah you cool kids, you can do that too
just in case the return value is "falsy" but still not undefined and therefore query-provided
e.g.
""
or 0