From 88fdb4148432f02c5cb8f9e30b09cfac6298fbad Mon Sep 17 00:00:00 2001 From: MiniPear Date: Mon, 11 Sep 2023 16:41:08 +0800 Subject: [PATCH] fix(types): composition node (#5527) --- __tests__/unit/api/type.spec.ts | 15 +++++++++++++++ src/api/chart.ts | 7 ++++++- src/api/extend.ts | 22 +++++++++++----------- src/api/index.ts | 9 ++++++--- src/index.ts | 4 ++-- 5 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 __tests__/unit/api/type.spec.ts diff --git a/__tests__/unit/api/type.spec.ts b/__tests__/unit/api/type.spec.ts new file mode 100644 index 0000000000..d8f3270316 --- /dev/null +++ b/__tests__/unit/api/type.spec.ts @@ -0,0 +1,15 @@ +import { Chart, CompositionNode, MarkNode } from '../../../src'; + +describe('types', () => { + const chart = new Chart(); + + test('CompositionNode', () => { + const rect: CompositionNode = chart.facetRect(); + rect.boxplot(); + }); + + test('MarkNode', () => { + const interval: MarkNode = chart.interval(); + interval.encode('x', 'name'); + }); +}); diff --git a/src/api/chart.ts b/src/api/chart.ts index 59924cbd5c..54bea9cb12 100644 --- a/src/api/chart.ts +++ b/src/api/chart.ts @@ -1,11 +1,16 @@ import { G2Spec } from '../spec'; -import { extend, API } from './extend'; +import { extend, API, CompositionAPI } from './extend'; import { library } from './library'; import { Runtime } from './runtime'; +import { MarkNode as Node } from './mark'; import type { RuntimeOptions } from './runtime'; export interface Chart extends API {} +export interface CompositionNode extends CompositionAPI {} + +export interface MarkNode extends Node {} + export const Chart = extend(Runtime, library); export type ChartOptions = Omit; diff --git a/src/api/extend.ts b/src/api/extend.ts index 3f85c0f213..32469a4dd7 100644 --- a/src/api/extend.ts +++ b/src/api/extend.ts @@ -5,22 +5,22 @@ import { MarkOf, CompositionOf as Of } from './types'; import { MarkNode } from './mark'; import { CompositionNode } from './composition'; -type CompositionOf = Of< - Library, - ( - composite?, - ) => CompositionNode< - CompositionOf & MarkOf MarkNode> - > & - MarkOf MarkNode> & - CompositionOf ->; - type BuiltinComposition = { 'composition.mark': any; 'composition.view': any; }; +export type CompositionOf = Of< + Library, + (composite?) => CompositionAPI +>; + +export type CompositionAPI = CompositionNode< + CompositionOf & MarkOf MarkNode> +> & + MarkOf MarkNode> & + CompositionOf; + export type API = Runtime & MarkOf MarkNode> & CompositionOf; diff --git a/src/api/index.ts b/src/api/index.ts index 8802486466..db6fc5d463 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,6 +1,9 @@ -export { Chart, type ChartOptions } from './chart'; -export { MarkNode } from './mark'; -export { CompositionNode } from './composition'; +export { + Chart, + type ChartOptions, + type MarkNode, + type CompositionNode, +} from './chart'; export { register } from './library'; export { Runtime } from './runtime'; export { extend } from './extend'; diff --git a/src/index.ts b/src/index.ts index 180cd251ba..b300c7e492 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,8 +30,8 @@ export * from './mark'; export { Chart, - MarkNode, - CompositionNode, + type MarkNode, + type CompositionNode, register, Runtime, extend,