UNPKG

2.72 kBTypeScriptView Raw
1type ElementOf<T> = T extends Array<any> ? T[number] : never;
2type ValueOf<T> = T[keyof T];
3export type Concrete<T> = {
4 [Key in keyof T]-?: T[Key];
5};
6/**
7 * If typeof `Value` is value, returns `Node`.
8 * Otherwise returns the value.
9 */
10export type ValueAttribute<Value, Node> = (() => Value) & ((value: Value) => Node);
11/**
12 * If no params, returns `Value`.
13 * Otherwise returns the `Node`.
14 */
15export type ObjectAttribute<Value, Node> = (() => Value) & (<K extends Value extends Record<string, any> ? Value : never>(key: K | keyof K | boolean) => Node) & (<K extends Value extends Record<string, any> ? keyof Value : never, V extends Value extends Record<string, any> ? ValueOf<Value> : never>(key: K, value: V | boolean) => Node);
16/**
17 * If typeof `Value` is an array or an element of array, returns `Node`.
18 * Otherwise returns the array.
19 */
20export type ArrayAttribute<Value, Node> = (() => Value) & ((value: Value | ElementOf<Value>) => Node);
21/**
22 * Filter Object keys with the specified prefix.
23 * @example
24 * type A = { 'mark.a': number, 'composition.b': number }
25 * type B = FilterKey<A, 'mark'>; // { 'a': number }
26 */
27export type FilterKey<T extends string | symbol | number, Prefix extends string> = T extends `${Prefix}.${infer Key}` ? Key : never;
28/**
29 * Filter Object keys with `mark` as prefix.
30 * @example
31 * type A = { 'mark.a': number, 'composition.b': number }
32 * type B = FilterKey<A, 'mark'>; // { 'a': number }
33 * @todo Remove component.xxxx
34 */
35export type FilterMark<T extends string | symbol | number> = T extends 'component.axisX' ? 'axisX' : T extends 'component.axisY' ? 'axisY' : T extends 'component.legends' ? 'legends' : FilterKey<T, 'mark'>;
36/**
37 * Filter Object keys with `composition` as prefix.
38 * @example
39 * type A = { 'mark.a': number, 'composition.b': number }
40 * type B = FilterComposition<A>; // { 'b': number }
41 */
42export type FilterComposition<T extends string | symbol | number> = FilterKey<T, 'composition'>;
43/**
44 * Map marks of library to Nodes.
45 */
46export type MarkOf<Library extends Record<string, any>, Node> = {
47 [Key in keyof Library as FilterMark<Key>]: Node;
48};
49/**
50 * Map compositions of library to Nodes.
51 */
52export type CompositionOf<Library extends Record<string, any>, Node> = {
53 [Key in keyof Library as FilterComposition<Key>]: Node;
54};
55/**
56 * Map descriptors to props of Nodes.
57 */
58export type PropsOf<Descriptor extends Record<string, any>, Spec extends Record<any, any>, Node> = {
59 [Key in keyof Descriptor]: Descriptor[Key] extends {
60 type: 'object';
61 } ? ObjectAttribute<Spec[Key], Node> : Descriptor[Key] extends {
62 type: 'value';
63 } ? ValueAttribute<Spec[Key], Node> : ArrayAttribute<Spec[Key], Node>;
64};
65export {};
66
\No newline at end of file