Skip to content

Commit 3460ef1

Browse files
authored
Types: Add couple of tests, move utils to new file (#8526)
1 parent bd303f4 commit 3460ef1

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

types/index.esm.d.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
* }
1313
*/
1414

15+
import { DeepPartial, DistributiveArray } from './utils';
16+
1517
import { TimeUnit } from './adapters';
1618
import { AnimationEvent } from './animation';
1719
import { AnyObject, EmptyObject } from './basic';
@@ -3085,21 +3087,6 @@ export const RadialLinearScale: ChartComponent & {
30853087
new <O extends RadialLinearScaleOptions = RadialLinearScaleOptions>(cfg: AnyObject): RadialLinearScale<O>;
30863088
};
30873089

3088-
// DeepPartial implementation taken from the utility-types NPM package, which is
3089-
// Copyright (c) 2016 Piotr Witek <piotrek.witek@gmail.com> (http://piotrwitek.github.io)
3090-
// and used under the terms of the MIT license
3091-
export type DeepPartial<T> = T extends Function
3092-
? T
3093-
: T extends Array<infer U>
3094-
? _DeepPartialArray<U>
3095-
: T extends object
3096-
? _DeepPartialObject<T>
3097-
: T | undefined;
3098-
type _DeepPartialArray<T> = Array<DeepPartial<T>>
3099-
type _DeepPartialObject<T> = { [P in keyof T]?: DeepPartial<T[P]> };
3100-
3101-
export type DistributiveArray<T> = T extends unknown ? T[] : never
3102-
31033090
export interface CartesianScaleTypeRegistry {
31043091
linear: {
31053092
options: LinearScaleOptions;
@@ -3254,7 +3241,6 @@ export type DefaultDataPoint<TType extends ChartType> = DistributiveArray<ChartT
32543241

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

3257-
32583244
export interface ChartDatasetProperties<TType extends ChartType, TData> {
32593245
type?: TType;
32603246
data: TData;

types/tests/parsed.data.type.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { ParsedDataType } from '../index.esm';
2+
3+
interface test {
4+
pie: ParsedDataType<'pie'>,
5+
line: ParsedDataType<'line'>,
6+
testA: ParsedDataType<'pie' | 'line' | 'bar'>
7+
testB: ParsedDataType<'pie' | 'line' | 'bar'>
8+
testC: ParsedDataType<'pie' | 'line' | 'bar'>
9+
}
10+
11+
export const testImpl: test = {
12+
pie: 1,
13+
line: { x: 1, y: 2 },
14+
testA: 1,
15+
testB: { x: 1, y: 2 },
16+
// @ts-expect-error testC should be limited to pie/line datatypes
17+
testC: 'test'
18+
};

types/tests/scriptable.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Scriptable } from '../index.esm';
2+
3+
interface test {
4+
pie?: Scriptable<number, 'pie'>,
5+
line?: Scriptable<number, 'line'>,
6+
testA?: Scriptable<number, 'pie' | 'line' | 'bar'>
7+
testB?: Scriptable<number, 'pie' | 'line' | 'bar'>
8+
testC?: Scriptable<number, 'pie' | 'line' | 'bar'>
9+
}
10+
11+
const pieScriptable: Scriptable<number, 'pie'> = (ctx) => ctx.parsed;
12+
const lineScriptable: Scriptable<number, 'line'> = (ctx) => ctx.parsed.x + ctx.parsed.y;
13+
14+
export const testImpl: test = {
15+
pie: (ctx) => ctx.parsed,
16+
line: (ctx) => ctx.parsed.x + ctx.parsed.y,
17+
testA: pieScriptable,
18+
testB: lineScriptable,
19+
// @FIXME ts-expect-error combined type should not be any
20+
testC: (ctx) => ctx.fail
21+
};

types/utils.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
// DeepPartial implementation taken from the utility-types NPM package, which is
3+
// Copyright (c) 2016 Piotr Witek <piotrek.witek@gmail.com> (http://piotrwitek.github.io)
4+
// and used under the terms of the MIT license
5+
export type DeepPartial<T> = T extends Function
6+
? T
7+
: T extends Array<infer U>
8+
? _DeepPartialArray<U>
9+
: T extends object
10+
? _DeepPartialObject<T>
11+
: T | undefined;
12+
type _DeepPartialArray<T> = Array<DeepPartial<T>>
13+
type _DeepPartialObject<T> = { [P in keyof T]?: DeepPartial<T[P]> };
14+
15+
export type DistributiveArray<T> = T extends unknown ? T[] : never

0 commit comments

Comments
 (0)