Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: multitype charts typings #792

Merged
merged 1 commit into from
Oct 20, 2021
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
19 changes: 10 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ import type {
export interface Props<
TType extends ChartType = ChartType,
TData = DefaultDataPoint<TType>,
TLabel = unknown,
TOtherType extends TType = TType
TLabel = unknown
> extends CanvasHTMLAttributes<HTMLCanvasElement> {
type: TType;
/**
* @todo Remove function variant.
*/
data:
| ChartData<TOtherType, TData, TLabel>
| ((canvas: HTMLCanvasElement) => ChartData<TOtherType, TData, TLabel>);
options?: ChartOptions<TOtherType>;
plugins?: Plugin<TOtherType>[];
| ChartData<TType, TData, TLabel>
| ((canvas: HTMLCanvasElement) => ChartData<TType, TData, TLabel>);
options?: ChartOptions<TType>;
plugins?: Plugin<TType>[];
redraw?: boolean;
/**
* @todo Replace with `children` prop.
Expand Down Expand Up @@ -66,10 +68,9 @@ export type TypedChartComponent<
: <
TType extends ChartType = ChartType,
TData = DefaultDataPoint<TType>,
TLabel = unknown,
TOtherType extends TType = TType
TLabel = unknown
>(
props: Props<TType, TData, TLabel, TOtherType> & {
props: Props<TType, TData, TLabel> & {
ref?: ForwardedRef<ChartJSOrUndefined<TType, TData, TLabel>>;
}
) => JSX.Element;
6 changes: 3 additions & 3 deletions stories/Chart.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const multiTypeData = {
labels: months,
datasets: [
{
type: 'line',
type: 'line' as const,
label: 'Dataset 1',
borderColor: colorRed,
borderWidth: 2,
Expand All @@ -15,7 +15,7 @@ export const multiTypeData = {
),
},
{
type: 'bar',
type: 'bar' as const,
label: 'Dataset 2',
backgroundColor: colorGreen,
data: Array.from({ length: 7 }, () =>
Expand All @@ -25,7 +25,7 @@ export const multiTypeData = {
borderWidth: 2,
},
{
type: 'bar',
type: 'bar' as const,
label: 'Dataset 3',
backgroundColor: colorBlue,
data: Array.from({ length: 7 }, () =>
Expand Down
4 changes: 3 additions & 1 deletion test/chart.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expectError } from 'tsd';
import React from 'react';
import { Plugin } from 'chart.js';
import Chart, { Scatter, Doughnut } from '../src';
import { multiTypeData } from '../stories/Chart.data';

const data = {
datasets: [],
Expand All @@ -13,8 +14,9 @@ const data = {

<Chart type='radar' data={data} plugins={[] as Plugin<'radar'>[]} />;
<Scatter data={data} plugins={[] as Plugin<'scatter'>[]} />;
<Chart type='bar' data={multiTypeData} />;
<Chart type='scatter' data={data} plugins={[] as Plugin<'bar'>[]} />;

expectError(<Chart type='radar' data={data} plugins={[] as Plugin<'bar'>[]} />);
expectError(<Scatter data={data} plugins={[] as Plugin<'bar'>[]} />);

/**
Expand Down