Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/date-data-getter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"victory-core": patch
---

`DataGetterPropType` (the type of `x`, `y`, `y0` and `sortKey`) accepts accessors that return a `Date`, such as `x={(d) => new Date(d.date)}`. These worked at runtime but failed to type-check (TS2769).
13 changes: 9 additions & 4 deletions packages/victory-core/src/types/prop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,15 @@ export type CategoryPropType =
y: string[];
};

export type DataGetterPropType = ValueOrAccessor<
string | string[] | number | number[],
any // The arg will be the `datum`, which can have any type
>;
// A value is a property key, path, or array index into each datum; an
// accessor can also return a `Date` (for time scales).
export type DataGetterPropType =
| string
| string[]
| number
| number[]
// The arg will be the `datum`, which can have any type
| ((datum: any) => string | string[] | number | number[] | Date);

export type InterpolationPropType =
| "basis"
Expand Down
10 changes: 3 additions & 7 deletions website/src/pages/_components/landing-demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ const VoronoiLabel = (props) => {
);
};

// Victory accepts Date accessors at runtime, but its accessor type
// (DataGetterPropType) leaves out Date. Drop this once #3125 is fixed.
const dateX = (date: string) => new Date(date) as unknown as number;

const lastDate = last(downloads.data)!.day;
const recentDate = format(subDays(new Date(), 2), "yyyy-MM-dd");
const oldDownloads = groupDownloadsByWeek(downloads.data);
Expand Down Expand Up @@ -198,7 +194,7 @@ export const LandingDemo = () => {
<VictoryLine
name={`ignore-${v.version}`}
key={v.version}
x={() => dateX(v.date)}
x={() => new Date(v.date)}
style={{
data: {
stroke: theme.color.red,
Expand All @@ -215,7 +211,7 @@ export const LandingDemo = () => {
data={downloadsPerWeek}
groupComponent={<g />}
y="downloads"
x={(d) => dateX(d.date)}
x={(d) => new Date(d.date)}
style={{
data: { stroke: theme.color.white, strokeWidth: 4 },
}}
Expand All @@ -224,7 +220,7 @@ export const LandingDemo = () => {
name="ignore-scatter"
data={[last(downloadsPerWeek)]}
y="downloads"
x={(d) => dateX(d.date)}
x={(d) => new Date(d.date)}
size={6}
style={{
data: { fill: theme.color.white },
Expand Down
Loading