This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
/
index.d.ts
268 lines (222 loc) · 5.9 KB
/
index.d.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
type Predicate<T> = (x: T) => boolean;
type TypeGuardPredicate<T> = (x: any) => x is T;
interface Type<T> extends Function {
(value: T): T;
is: TypeGuardPredicate<T>;
displayName: string;
meta: {
kind: string;
name: string;
identity: boolean;
};
t: T;
}
//
// irreducible
//
interface Irreducible<T> extends Type<T> {
meta: {
kind: string;
name: string;
identity: boolean;
predicate: TypeGuardPredicate<T>;
};
}
export function irreducible<T>(name: string, predicate: Predicate<any>): Irreducible<T>;
//
// basic types
//
export var Any: Irreducible<any>;
export var Nil: Irreducible<void | null>;
export var String: Irreducible<string>;
export var Number: Irreducible<number>;
export var Integer: Irreducible<number>;
export var Boolean: Irreducible<boolean>;
export var Array: Irreducible<Array<any>>;
export var Object: Irreducible<Object>; // FIXME restrict to POJOs
export var Function: Irreducible<Function>;
export var Error: Irreducible<Error>;
export var RegExp: Irreducible<RegExp>;
export var Date: Irreducible<Date>;
interface ApplyCommand { $apply: Function; }
interface PushCommand { $push: Array<any>; }
interface RemoveCommand { $remove: Array<string>; }
interface SetCommand { $set: any; }
interface SpliceCommand { $splice: Array<Array<any>>; }
interface SwapCommand { $swap: { from: number; to: number; }; }
interface UnshiftCommand { $unshift: Array<any>; }
interface MergeCommand { $merge: Object; }
type Command = ApplyCommand | PushCommand | RemoveCommand | SetCommand | SpliceCommand | SwapCommand | UnshiftCommand | MergeCommand;
type UpdatePatch = Command | { [key: string]: UpdatePatch };
type Update<T> = (instance: T, spec: UpdatePatch) => T;
type Constructor<T> = Type<T> | Function;
//
// refinement
//
interface Refinement<T> extends Type<T> {
meta: {
kind: string;
name: string;
identity: boolean;
type: Constructor<T>;
predicate: TypeGuardPredicate<T>;
};
update: Update<T>;
}
export function refinement<T>(type: Constructor<T>, predicate: Predicate<T>, name?: string): Refinement<T>;
//
// struct
//
type StructProps = { [key: string]: Constructor<any> };
type StructMixin = StructProps | Struct<any> | Interface<any>;
interface Struct<T> extends Type<T> {
new(value: T): T;
meta: {
kind: string;
name: string;
identity: boolean;
props: StructProps;
strict: boolean;
};
update: Update<T>;
extend<E extends T>(mixins: StructMixin | Array<StructMixin>, name?: string): Struct<E>;
}
type StructOptions = {
name?: string,
strict?: boolean
};
export function struct<T>(props: StructProps, name?: string | StructOptions): Struct<T>;
//
// interface
//
interface Interface<T> extends Type<T> {
meta: {
kind: string;
name: string;
identity: boolean;
props: StructProps;
strict: boolean;
};
update: Update<T>;
extend<E extends T>(mixins: StructMixin | Array<StructMixin>, name?: string): Struct<E>;
}
export function interface<T>(props: StructProps, name?: string | StructOptions): Interface<T>;
//
// list
//
interface List<T> extends Type<Array<T>> {
meta: {
kind: string;
name: string;
identity: boolean;
type: Constructor<T>;
};
update: Update<Array<T>>;
}
export function list<T>(type: Constructor<T>, name?: string): List<T>;
//
// dict combinator
//
interface Dict<T> extends Type<{ [key: string]: T; }> {
meta: {
kind: string;
name: string;
identity: boolean;
domain: Constructor<string>;
codomain: T;
};
update: Update<{ [key: string]: T; }>;
}
export function dict<T>(domain: Constructor<string>, codomain: Constructor<T>, name?: string): Dict<T>;
//
// enums combinator
//
interface Enums extends Type<string> {
meta: {
kind: string;
name: string;
identity: boolean;
map: Object;
};
}
interface EnumsFunction extends Function {
(map: Object, name?: string): Enums;
of(enums: string, name?: string): Enums;
of(enums: Array<string>, name?: string): Enums;
}
export var enums: EnumsFunction;
//
// maybe combinator
//
interface Maybe<T> extends Type<void | T> {
meta: {
kind: string;
name: string;
identity: boolean;
type: Constructor<T>;
};
update: Update<void | T>;
}
export function maybe<T>(type: Constructor<T>, name?: string): Maybe<T>;
//
// tuple combinator
//
interface Tuple<T> extends Type<T> {
meta: {
kind: string;
name: string;
identity: boolean;
types: Array<Constructor<any>>;
};
update: Update<T>;
}
export function tuple<T>(types: Array<Constructor<any>>, name?: string): Tuple<T>;
//
// union combinator
//
interface Union<T> extends Type<T> {
meta: {
kind: string;
name: string;
identity: boolean;
types: Array<Constructor<T>>;
};
update: Update<T>;
dispatch(x: any): Constructor<T>;
}
export function union<T>(types: Array<Constructor<T>>, name?: string): Union<T>;
//
// intersection combinator
//
interface Intersection<T> extends Type<T> {
meta: {
kind: string;
name: string;
identity: boolean;
types: Array<Constructor<any>>;
};
update: Update<T>;
}
export function intersection<T>(types: Array<Constructor<any>>, name?: string): Intersection<T>;
//
// declare combinator
//
interface Declare<T> extends Type<T> {
update: Update<T>;
define(type: Type<any>): void;
}
export function declare<T>(name?: string): Declare<T>;
//
// other exports
//
export function is<T>(x: any, type: Constructor<T>): boolean;
type LazyMessage = () => string;
export function assert(guard: boolean, message?: string | LazyMessage): void;
export function fail(message: string): void;
export function isType<T>(x: Constructor<T>): boolean;
export function getTypeName<T>(x: Constructor<T>): string;
export function mixin<T, S>(target: T, source: S, overwrite?: boolean): T & S;
type Function1 = (x: any) => any;
type Clause = Constructor<any> | Function1;
export function match(x: any, ...clauses: Array<Clause>): any; // FIXME
export var update: Update<Object>;