From: Jukka Kurkela Date: Fri, 26 Feb 2021 21:29:10 +0000 (+0200) Subject: Types: Add couple of tests, move utils to new file (#8526) X-Git-Tag: v3.0.0-beta.12~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3460ef126e77c9a6ab83f5f03cdcc28e1aaeec84;p=thirdparty%2FChart.js.git Types: Add couple of tests, move utils to new file (#8526) --- diff --git a/types/index.esm.d.ts b/types/index.esm.d.ts index a72156a6c..b5fb8e5c8 100644 --- a/types/index.esm.d.ts +++ b/types/index.esm.d.ts @@ -12,6 +12,8 @@ * } */ +import { DeepPartial, DistributiveArray } from './utils'; + import { TimeUnit } from './adapters'; import { AnimationEvent } from './animation'; import { AnyObject, EmptyObject } from './basic'; @@ -3085,21 +3087,6 @@ export const RadialLinearScale: ChartComponent & { new (cfg: AnyObject): RadialLinearScale; }; -// DeepPartial implementation taken from the utility-types NPM package, which is -// Copyright (c) 2016 Piotr Witek (http://piotrwitek.github.io) -// and used under the terms of the MIT license -export type DeepPartial = T extends Function - ? T - : T extends Array - ? _DeepPartialArray - : T extends object - ? _DeepPartialObject - : T | undefined; - type _DeepPartialArray = Array> - type _DeepPartialObject = { [P in keyof T]?: DeepPartial }; - -export type DistributiveArray = T extends unknown ? T[] : never - export interface CartesianScaleTypeRegistry { linear: { options: LinearScaleOptions; @@ -3254,7 +3241,6 @@ export type DefaultDataPoint = DistributiveArray = ChartTypeRegistry[TType]['parsedDataType']; - export interface ChartDatasetProperties { type?: TType; data: TData; diff --git a/types/tests/parsed.data.type.ts b/types/tests/parsed.data.type.ts new file mode 100644 index 000000000..a0357f095 --- /dev/null +++ b/types/tests/parsed.data.type.ts @@ -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' +}; diff --git a/types/tests/scriptable.ts b/types/tests/scriptable.ts new file mode 100644 index 000000000..7fde9acb2 --- /dev/null +++ b/types/tests/scriptable.ts @@ -0,0 +1,21 @@ +import { Scriptable } from '../index.esm'; + +interface test { + pie?: Scriptable, + line?: Scriptable, + testA?: Scriptable + testB?: Scriptable + testC?: Scriptable +} + +const pieScriptable: Scriptable = (ctx) => ctx.parsed; +const lineScriptable: Scriptable = (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 +}; diff --git a/types/utils.d.ts b/types/utils.d.ts new file mode 100644 index 000000000..6cfbd7d85 --- /dev/null +++ b/types/utils.d.ts @@ -0,0 +1,15 @@ + +// DeepPartial implementation taken from the utility-types NPM package, which is +// Copyright (c) 2016 Piotr Witek (http://piotrwitek.github.io) +// and used under the terms of the MIT license +export type DeepPartial = T extends Function + ? T + : T extends Array + ? _DeepPartialArray + : T extends object + ? _DeepPartialObject + : T | undefined; + type _DeepPartialArray = Array> +type _DeepPartialObject = { [P in keyof T]?: DeepPartial }; + +export type DistributiveArray = T extends unknown ? T[] : never