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
18 changes: 2 additions & 16 deletions types/index.esm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* }
*/

import { DeepPartial, DistributiveArray } from './utils';

import { TimeUnit } from './adapters';
import { AnimationEvent } from './animation';
import { AnyObject, EmptyObject } from './basic';
Expand Down Expand Up @@ -3085,21 +3087,6 @@ export const RadialLinearScale: ChartComponent & {
new <O extends RadialLinearScaleOptions = RadialLinearScaleOptions>(cfg: AnyObject): RadialLinearScale<O>;
};

// DeepPartial implementation taken from the utility-types NPM package, which is
// Copyright (c) 2016 Piotr Witek <piotrek.witek@gmail.com> (http://piotrwitek.github.io)
// and used under the terms of the MIT license
export type DeepPartial<T> = T extends Function
? T
: T extends Array<infer U>
? _DeepPartialArray<U>
: T extends object
? _DeepPartialObject<T>
: T | undefined;
type _DeepPartialArray<T> = Array<DeepPartial<T>>
type _DeepPartialObject<T> = { [P in keyof T]?: DeepPartial<T[P]> };

export type DistributiveArray<T> = T extends unknown ? T[] : never

export interface CartesianScaleTypeRegistry {
linear: {
options: LinearScaleOptions;
Expand Down Expand Up @@ -3254,7 +3241,6 @@ export type DefaultDataPoint<TType extends ChartType> = DistributiveArray<ChartT

export type ParsedDataType<TType extends ChartType = ChartType> = ChartTypeRegistry[TType]['parsedDataType'];


export interface ChartDatasetProperties<TType extends ChartType, TData> {
type?: TType;
data: TData;
Expand Down
18 changes: 18 additions & 0 deletions types/tests/parsed.data.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ParsedDataType } from '../index.esm';

interface test {
pie: ParsedDataType<'pie'>,
line: ParsedDataType<'line'>,
testA: ParsedDataType<'pie' | 'line' | 'bar'>
testB: ParsedDataType<'pie' | 'line' | 'bar'>
testC: ParsedDataType<'pie' | 'line' | 'bar'>
}

export const testImpl: test = {
pie: 1,
line: { x: 1, y: 2 },
testA: 1,
testB: { x: 1, y: 2 },
// @ts-expect-error testC should be limited to pie/line datatypes
testC: 'test'
};
21 changes: 21 additions & 0 deletions types/tests/scriptable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Scriptable } from '../index.esm';

interface test {
pie?: Scriptable<number, 'pie'>,
line?: Scriptable<number, 'line'>,
testA?: Scriptable<number, 'pie' | 'line' | 'bar'>
testB?: Scriptable<number, 'pie' | 'line' | 'bar'>
testC?: Scriptable<number, 'pie' | 'line' | 'bar'>
}

const pieScriptable: Scriptable<number, 'pie'> = (ctx) => ctx.parsed;
const lineScriptable: Scriptable<number, 'line'> = (ctx) => ctx.parsed.x + ctx.parsed.y;

export const testImpl: test = {
pie: (ctx) => ctx.parsed,
line: (ctx) => ctx.parsed.x + ctx.parsed.y,
testA: pieScriptable,
testB: lineScriptable,
// @FIXME ts-expect-error combined type should not be any
testC: (ctx) => ctx.fail
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, the combined type here should be number | CartesianParsedData | BarParsedData

};
15 changes: 15 additions & 0 deletions types/utils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

// DeepPartial implementation taken from the utility-types NPM package, which is
// Copyright (c) 2016 Piotr Witek <piotrek.witek@gmail.com> (http://piotrwitek.github.io)
// and used under the terms of the MIT license
export type DeepPartial<T> = T extends Function
? T
: T extends Array<infer U>
? _DeepPartialArray<U>
: T extends object
? _DeepPartialObject<T>
: T | undefined;
type _DeepPartialArray<T> = Array<DeepPartial<T>>
type _DeepPartialObject<T> = { [P in keyof T]?: DeepPartial<T[P]> };

export type DistributiveArray<T> = T extends unknown ? T[] : never