Skip to content

Commit

Permalink
fix(types): composition node (#5527)
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini authored Sep 11, 2023
1 parent ab59152 commit 88fdb41
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
15 changes: 15 additions & 0 deletions __tests__/unit/api/type.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
7 changes: 6 additions & 1 deletion src/api/chart.ts
Original file line number Diff line number Diff line change
@@ -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<G2Spec, typeof library> {}

export interface CompositionNode extends CompositionAPI<typeof library> {}

export interface MarkNode extends Node {}

export const Chart = extend(Runtime, library);

export type ChartOptions = Omit<RuntimeOptions, 'lib'>;
22 changes: 11 additions & 11 deletions src/api/extend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import { MarkOf, CompositionOf as Of } from './types';
import { MarkNode } from './mark';
import { CompositionNode } from './composition';

type CompositionOf<Library> = Of<
Library,
(
composite?,
) => CompositionNode<
CompositionOf<Library> & MarkOf<Library, (composite?) => MarkNode>
> &
MarkOf<Library, (composite?) => MarkNode> &
CompositionOf<Library>
>;

type BuiltinComposition = {
'composition.mark': any;
'composition.view': any;
};

export type CompositionOf<Library> = Of<
Library,
(composite?) => CompositionAPI<Library>
>;

export type CompositionAPI<Library> = CompositionNode<
CompositionOf<Library> & MarkOf<Library, (composite?) => MarkNode>
> &
MarkOf<Library, (composite?) => MarkNode> &
CompositionOf<Library>;

export type API<Spec, Library> = Runtime<Spec> &
MarkOf<Library, (composite?) => MarkNode> &
CompositionOf<Library & BuiltinComposition>;
Expand Down
9 changes: 6 additions & 3 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -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';
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export * from './mark';

export {
Chart,
MarkNode,
CompositionNode,
type MarkNode,
type CompositionNode,
register,
Runtime,
extend,
Expand Down

0 comments on commit 88fdb41

Please sign in to comment.