Types: Change context.chart to plain Chart (#9477)

This commit is contained in:
Jukka Kurkela 2021-07-27 15:26:54 +03:00 committed by GitHub
parent e8f2a5a271
commit 243c9707d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 4 additions and 13 deletions

View File

@ -1,4 +1,4 @@
import { DeepPartial, DistributiveArray, IntersectItems, UnionToIntersection } from './utils';
import { DeepPartial, DistributiveArray, UnionToIntersection } from './utils';
import { TimeUnit } from './adapters';
import { AnimationEvent } from './animation';
@ -17,7 +17,7 @@ export { LayoutItem, LayoutPosition } from './layout';
export interface ScriptableContext<TType extends ChartType> {
active: boolean;
chart: IntersectItems<Chart<TType>>;
chart: Chart;
dataIndex: number;
dataset: UnionToIntersection<ChartDataset<TType>>;
datasetIndex: number;

View File

@ -10,9 +10,9 @@ interface test {
}
const testImpl: test = {
pie: (ctx) => ctx.parsed,
pie: (ctx) => ctx.parsed + ctx.chart.width,
line: (ctx) => ctx.parsed.x + ctx.parsed.y,
testA: (ctx) => ctx.parsed,
testA: (ctx) => ctx.parsed + ctx.dataset.data[0],
testB: (ctx) => ctx.parsed.x + ctx.parsed.y,
// @ts-expect-error combined type should not be any
testC: (ctx) => ctx.fail,

9
types/utils.d.ts vendored
View File

@ -19,12 +19,3 @@ export type DistributiveArray<T> = [T] extends [unknown] ? Array<T> : never
// https://stackoverflow.com/a/50375286
export type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
// https://stackoverflow.com/a/59463385
type TupleKeys<T extends unknown> = Exclude<keyof T, keyof []>
type Foo<T extends unknown> = {
[K in TupleKeys<T>]: {foo: T[K]}
}
type Values<T> = T[keyof T]
type Unfoo<T> = T extends { foo: unknown } ? T['foo'] : never;
export type IntersectItems<T extends unknown> = Unfoo<UnionToIntersection<Values<Foo<T>>>>;