-
Notifications
You must be signed in to change notification settings - Fork 146
/
Definition.ts
118 lines (103 loc) · 3.15 KB
/
Definition.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import type { DefinitionType } from './DefinitionType';
/**
* A type template to get item type of an array
*/
export type ArrayItemType<T extends any[]> = T extends (infer U)[] ? U : never;
/**
* Base interface of property definition
*/
export interface DefinitionBase<T extends DefinitionType> {
/**
* Type of this property
*/
type: T;
/**
* Whether this property is optional
*/
isOptional?: boolean;
/**
* Whether this property is allowed to be null
*/
allowNull?: boolean;
}
/**
* String property definition. This definition can also be used for string based enum property
*/
export interface StringDefinition extends DefinitionBase<'string'> {
/**
* An optional value of this property. When specified, the given property must have exactly same value of this value
*/
value?: string;
}
/**
* Number property definition. This definition can also be used for number based enum property
*/
export interface NumberDefinition extends DefinitionBase<'number'> {
/**
* An optional value of this property. When specified, the given property must have same value of this value
*/
value?: number;
/**
* An optional minimum value of this property. When specified, the given property must be greater or equal to this value
*/
minValue?: number;
/**
* An optional maximum value of this property. When specified, the given property must be less or equal to this value
*/
maxValue?: number;
}
/**
* Boolean property definition
*/
export interface BooleanDefinition extends DefinitionBase<'boolean'> {
/**
* An optional value of this property. When specified, the given property must have same value of this value
*/
value?: boolean;
}
/**
* Array property definition.
*/
export interface ArrayDefinition<T extends any[]> extends DefinitionBase<'array'> {
/**
* Definition of each item of this array. All items of the given array must have the same type. Otherwise, use CustomizeDefinition instead.
*/
itemDef: Definition<ArrayItemType<T>>;
/**
* An optional minimum length of this array. When specified, the given array must have at least this value of items
*/
minLength?: number;
/**
* An optional maximum length of this array. When specified, the given array must have at most this value of items
*/
maxLength?: number;
}
/**
* Object property definition type used by Object Definition
*/
export type ObjectPropertyDefinition<T extends Object> = {
[Key in keyof T]: Definition<T[Key]>;
};
/**
* Object property definition.
*/
export interface ObjectDefinition<T extends Object> extends DefinitionBase<'object'> {
/**
* A key-value map to specify the definition of each possible property of this object
*/
propertyDef: ObjectPropertyDefinition<T>;
}
/**
* A combination of all definition types
*/
export type Definition<T> = T extends any[]
? ArrayDefinition<T>
: T extends Record<string, any>
? ObjectDefinition<T>
: T extends String
? StringDefinition
: T extends Number
? NumberDefinition
: T extends Boolean
? BooleanDefinition
: never;