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
8 changes: 8 additions & 0 deletions .changeset/declaration-errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"victory-axis": patch
"victory-core": patch
"victory-create-container": patch
"victory-errorbar": patch
---

Fix type errors in published declarations under `skipLibCheck: false`. `createContainer` and `makeCreateContainerFunction` now have explicit return types (TS2344 before), with the same props as before. `ErrorBar`'s props are `ErrorBarProps` instead of `any` (`typeof ErrorBar.default`, TS2339 before). Code that passed `ErrorBar` undeclared props, or wrongly typed ones, compiled only because of this and now fails to type-check. `Axis.getTicks` returns `number[] | string[] | Date[]`: it returns `Date`s for date `tickValues`, which its inferred `number[] | string[]` left out (also `VictoryAxis`'s inferred `ticks`).
12 changes: 12 additions & 0 deletions packages/victory-core/src/victory-util/axis.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ describe("helpers/axis", () => {
expect(tickResult).toEqual([1, 2, 3, 4]);
});

it("returns Date tickValues as Dates", () => {
const tickValues = [
new Date(2020, 0, 1),
new Date(2021, 0, 1),
new Date(2022, 0, 1),
];
const timeScale = Scale.getBaseScale({ scale: { x: "time" } }, "x");
const tickResult = Axis.getTicks({ tickValues }, timeScale);
expect(tickResult).toEqual(tickValues);
expect(tickResult[0]).toBeInstanceOf(Date);
});

it("calculates tickValues from scale.ticks()", () => {
const props = { tickCount: 5 };
Axis.getTicks(props, scale);
Expand Down
6 changes: 5 additions & 1 deletion packages/victory-core/src/victory-util/axis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,11 @@ function downsampleTicks(ticks: number[], tickCount: number) {
return ticks.filter((d, i) => i % k === 0);
}

export function getTicks(props, scale: D3Scale, filterZero = false) {
export function getTicks(
props,
scale: D3Scale,
filterZero = false,
): number[] | string[] | Date[] {
const { tickCount } = props;
const tickArray = getTickArray(props);

Expand Down
69 changes: 48 additions & 21 deletions packages/victory-create-container/src/create-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,15 @@ const CONTAINER_HOOKS = {
* - These containers are specific to the web. Victory Native has its own container components.
* - For this utility, we only need the container components to extract the defaultEvents.
*/
const CONTAINER_COMPONENTS_WEB = {
interface WebContainerComponents {
zoom: typeof VictoryZoomContainer;
selection: typeof VictorySelectionContainer;
brush: typeof VictoryBrushContainer;
cursor: typeof VictoryCursorContainer;
voronoi: typeof VictoryVoronoiContainer;
}

const CONTAINER_COMPONENTS_WEB: WebContainerComponents = {
zoom: VictoryZoomContainer,
selection: VictorySelectionContainer,
brush: VictoryBrushContainer,
Expand All @@ -122,38 +130,59 @@ type ContainerComponents = Record<
}
>;

// Helper type to support backwards compatibility with old types
type CombinedContainerProps<
TContainerComponents extends ContainerComponents,
A,
B,
> = A extends ContainerType
? B extends ContainerType
? React.ComponentProps<TContainerComponents[A]> &
React.ComponentProps<TContainerComponents[B]> // New style: infer from container types
: A & B // Old style: expect props as generic types
: A & B;

interface CombinedContainer<TProps> {
(props: TProps): React.JSX.Element;
displayName: string;
role: string;
defaultEvents: (props: TProps) => ({
target: string;
eventHandlers: any;
} | null)[];
}

type CreateContainer<TContainerComponents extends ContainerComponents> = <
TContainerA extends ContainerType | VictoryContainerProps,
TContainerB extends ContainerType | VictoryContainerProps,
>(
containerA: TContainerA extends ContainerType ? TContainerA : ContainerType,
containerB: TContainerB extends ContainerType ? TContainerB : ContainerType,
) => CombinedContainer<
CombinedContainerProps<TContainerComponents, TContainerA, TContainerB>
>;

export function makeCreateContainerFunction<
TContainerComponents extends ContainerComponents,
>(
containerComponents: TContainerComponents,
VictoryContainerBase: typeof VictoryContainer,
) {
type ContainerProps<T extends ContainerType> = React.ComponentProps<
TContainerComponents[T]
>;

// Helper type to support backwards compatibility with old types
type CombinedContainerProps<A, B> = A extends ContainerType
? B extends ContainerType
? ContainerProps<A> & ContainerProps<B> // New style: infer from container types
: A & B // Old style: expect props as generic types
: A & B;
): CreateContainer<TContainerComponents> {
type Props<A, B> = CombinedContainerProps<TContainerComponents, A, B>;

return function combineContainers<
TContainerA extends ContainerType | VictoryContainerProps,
TContainerB extends ContainerType | VictoryContainerProps,
>(
containerA: TContainerA extends ContainerType ? TContainerA : ContainerType,
containerB: TContainerB extends ContainerType ? TContainerB : ContainerType,
) {
): CombinedContainer<Props<TContainerA, TContainerB>> {
const ContainerA = containerComponents[containerA as ContainerType];
const ContainerB = containerComponents[containerB as ContainerType];
const useContainerA = CONTAINER_HOOKS[containerA as ContainerType];
const useContainerB = CONTAINER_HOOKS[containerB as ContainerType];

const CombinedContainer = (
props: CombinedContainerProps<TContainerA, TContainerB>,
) => {
const CombinedContainer = (props: Props<TContainerA, TContainerB>) => {
const { children: childrenA, props: propsA } = useContainerA(props);
const { children: combinedChildren, props: combinedProps } =
useContainerB({
Expand All @@ -171,7 +200,7 @@ export function makeCreateContainerFunction<
CombinedContainer.displayName = `Victory${containerA}${containerB}Container`;
CombinedContainer.role = "container";
CombinedContainer.defaultEvents = (
props: CombinedContainerProps<TContainerA, TContainerB>,
props: Props<TContainerA, TContainerB>,
) =>
combineDefaultEvents([
...ContainerA.defaultEvents(props),
Expand All @@ -182,7 +211,5 @@ export function makeCreateContainerFunction<
};
}

export const createContainer = makeCreateContainerFunction(
CONTAINER_COMPONENTS_WEB,
VictoryContainer,
);
export const createContainer: CreateContainer<WebContainerComponents> =
makeCreateContainerFunction(CONTAINER_COMPONENTS_WEB, VictoryContainer);
8 changes: 1 addition & 7 deletions packages/victory-errorbar/src/error-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Line,
UserProps,
VictoryCommonPrimitiveProps,
EventsMixinClass,
} from "victory-core";

const renderBorder = (props, error, type) => {
Expand Down Expand Up @@ -104,19 +103,14 @@ export interface ErrorProps {
top?: { error: any; errorIndex: number };
}

// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface ErrorBar extends EventsMixinClass<ErrorBarProps> {}

const defaultProps = {
groupComponent: <g />,
lineComponent: <Line />,
role: "presentation",
shapeRendering: "auto",
};

export const ErrorBar = (
initialProps: ErrorBarProps & typeof ErrorBar.default,
) => {
export const ErrorBar = (initialProps: ErrorBarProps) => {
const props = evaluateProps(defaults({}, initialProps, defaultProps));
const { groupComponent } = props;
const userProps = UserProps.getSafeUserProps(props);
Expand Down
2 changes: 1 addition & 1 deletion packages/victory-errorbar/src/victory-errorbars.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ describe("components/victory-errorbar", () => {
<ErrorBar
data-testid="error-bar"
ariaLabel={({ datum }) => `error bar chart, x ${datum.x}`}
tabIndex={({ index }) => index + 2}
tabIndex={({ index }) => Number(index) + 2}
/>
}
/>,
Expand Down
26 changes: 7 additions & 19 deletions scripts/pack-check.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,13 @@ const ALLOWED_CJS_DEFAULT_IMPORTS = {
],
};

// Type errors in the published declarations that predate this check, by
// package, as `<published file>:<error code>` with how many there are, the
// same with `@types/react` 18 and 19. Fixing them changes published types
// (tracked in #3093). Anything else fails, and so does a count that no longer
// matches.
const ALLOWED_DECLARATION_ERRORS = {
"victory-create-container": {
// `createContainer`'s inferred return type indexes the container map with
// a type parameter that isn't narrowed to `ContainerType`.
"dist/create-container.d.cts:TS2344": 8,
"dist/create-container.d.mts:TS2344": 8,
},
"victory-errorbar": {
// `ErrorBar`'s props use `typeof ErrorBar.default`, which doesn't exist
// (it's `any` in the source).
"dist/error-bar.d.cts:TS2339": 1,
"dist/error-bar.d.mts:TS2339": 1,
},
};
// Known type errors in the published declarations, by package, as
// `<published file>:<error code>` with how many there are, the same with
// `@types/react` 18 and 19, e.g.
// `{ "victory-foo": { "dist/foo.d.mts:TS2344": 1 } }`. Anything else fails,
// and so does a count that no longer matches. Keep it empty: fix the source
// instead.
const ALLOWED_DECLARATION_ERRORS = {};

// Default imports in tsdown's ESM output, one per line: `D`, `D, { ... }` and
// `D, * as ns`, from a bare specifier. Revisit if the output format changes
Expand Down
Loading