Skip to content

fix(types): distribute ChartDataset over the chart type - #12342

Open
mrityunjay-pandey wants to merge 1 commit into
chartjs:masterfrom
mrityunjay-pandey:fix/chart-dataset-stable-type-ordering
Open

mrityunjay-pandey wants to merge 1 commit into
chartjs:masterfrom
mrityunjay-pandey:fix/chart-dataset-stable-type-ordering

Conversation

@mrityunjay-pandey

Copy link
Copy Markdown

Fixes #12285

ChartDataset<keyof ChartTypeRegistry> (and therefore ChartDataset with its default type argument) isn't assignable from the dataset of a single chart type with TypeScript 7, or with --stableTypeOrdering on TypeScript 6:

import {ChartDataset, ChartTypeRegistry} from 'chart.js';

const a: ChartDataset<keyof ChartTypeRegistry> = {} as ChartDataset<'scatter'>; // TS2322
const datasets: ChartDataset[] = [{} as ChartDataset<'line'>];                 // TS2322

For a union of chart types, the type was the intersection of three parts: a union of the per-type options, the plugin options of all types, and ChartDatasetProperties of the whole union. TypeScript has to match that intersection against each member of the options union. Whether it found the right member depended on the order of the union, so TS 6 accepted it only with its default ordering, and failed in the full type-test suite even then.

This makes ChartDataset distribute over TType. ChartDataset<'line' | 'scatter'> is now ChartDataset<'line'> | ChartDataset<'scatter'>, where each member has the options, plugin options and type of a single chart type. TData keeps its default for the whole union, so the data of each member is as wide as before. For a single chart type the result is the same type as before.

Tests

New test/types/chart_dataset_assignability.ts assigns ChartDataset<'scatter'>, ChartDataset<'line'> and ChartDataset<'bar', number[]> to ChartDataset<keyof ChartTypeRegistry>, ChartDataset and ChartDataset[].

  • pnpm lint-types (TypeScript 4.9 from the lockfile) passes.
  • I also compiled all of test/types against the built dist types with TypeScript 6.0.3, 6.0.3 --stableTypeOrdering and 7.0.2. The repo's tsconfig doesn't load on those versions (baseUrl, importsNotUsedAsValues), so I used a minimal one. On master the new test fails with all three. With this change it passes, and the rest of the suite reports exactly the same errors as on master (they come from strict in that minimal config), so no new errors.

With TypeScript 7, or --stableTypeOrdering on TypeScript 6, the dataset
of a single chart type was not assignable to ChartDataset<keyof
ChartTypeRegistry>, because the options, plugin options and properties
of all chart types were intersected into one type. Make ChartDataset a
union of complete per-type datasets instead.

Fixes chartjs#12285
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chart.JS 4.5.1 breaks with TS --stableTypeOrdering and TS 7.

1 participant