I have an event
type in Typescript that looks like this:
export type EventRecord = {
name: string;
eta: string | null;
assumed_time: string | null;
indicated_time: string | null;
};
and function that displays the time of that event:
export const displayTime = (event: EventRecord): string | null =>
event.indicated_time || event.assumed_time || event.eta;
What I want to achieve is to make sure that at least one of the three times (eta, assumed_time, indicated_time) is not null, so that when my event is Marked as done I'll be able to display that time on the timeline component. I filter my events with Remeda
to first, filter events tot only marked ones, and then displaying them:
const timelineEvents = R.pipe(
events,
R.filter(eventMarked),
R.sortBy(displayTime),
R.reverse
);
{timelineEvents.map((event: EventRecord) => {
const time = new Date(displayTime(event)!);
return (
<TimelineEntry
name={event.name}
time={time}
/>
</RecordContextProvider>
);
})}
Here is my eventMarked function, it basically check if there is at least one time provided:
export const eventMarked = (event: eventRecord): boolean =>
!!(event.assumed_time || event.indicated_time || event.eta);
The problem with this setup is that I keep getting errors:
S2345: Argument of type 'unknown[]' is not assignable to parameter of type '(input: EventRecord[]) => unknown'.
Type 'unknown[]' provides no match for the signature '(input: EventRecord[]): unknown'.
80 | events,
81 | R.filter(eventMarked),
> 82 | R.sortBy(displayTime),
| ^^^^^^^^^^^^^^^^^^^^^
83 | R.reverse
84 | );
TS2769: No overload matches this call.
Overload 1 of 2, '(array: readonly unknown[], ...sorts: SortRule<unknown>[]): unknown[]', gave the following error.
Argument of type '(event: EventRecord) => string | null' is not assignable to parameter of type 'readonly unknown[]'.
Overload 2 of 2, '(sort: SortRule<EventRecord>, ...sorts: SortRule<EventRecord>[]): (array: readonly EventRecord[]) => EventRecord[]', gave the following error.
Argument of type '(event: EventRecord) => string | null' is not assignable to parameter of type 'SortRule<EventRecord>'.
Type '(event: EventRecord) => string | null' is not assignable to type 'SortProjection<EventRecord>'.
Type 'string | null' is not assignable to type 'Comparable'.
Type 'null' is not assignable to type 'Comparable'.
80 | events,
81 | R.filter(eventMarked),
> 82 | R.sortBy(displayTime),
| ^^^^^^^^^^^
83 | R.reverse
84 | );
85 |
TS2339: Property 'map' does not exist on type '(array: readonly unknown[]) => unknown[]'.
114 | />
115 | )}
> 116 | {timelineEvents.map((event: EventRecord) => {
| ^^^
I think the cause of this error might be that typescript doesn't really know what type is EventRecord because display is either null or string. How can this problem be solved?