-
Notifications
You must be signed in to change notification settings - Fork 3k
/
types.ts
278 lines (230 loc) · 8.38 KB
/
types.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
269
270
271
272
273
274
275
276
277
278
// https://github.com/microsoft/TypeScript/issues/40462#issuecomment-689879308
/// <reference lib="esnext.asynciterable" />
import { Observable } from './Observable';
import { Subscription } from './Subscription';
/**
* NOTE: This will add Symbol.observable globally for all TypeScript users,
* however, we are no longer polyfilling Symbol.observable. Note that this will be at
* odds with older version of @types/node and symbol-observable which incorrectly define
* `Symbol.observable` as `symbol` rather than `unique symbol`. "What about not defining
* this non-standard symbol at all?" you might ask... Well, that ship has sailed. There are
* dozens of libraries using this symbol now and many of them are quite popular.
* So here we are, and it's probably my fault. Sorry, "the web", if I have hurt you,
* the world just needed a standard way to provide interop for these types. -Ben
*/
declare global {
interface SymbolConstructor {
readonly observable: unique symbol;
}
}
/** OPERATOR INTERFACES */
export interface UnaryFunction<T, R> {
(source: T): R;
}
export interface OperatorFunction<T, R> extends UnaryFunction<Observable<T>, Observable<R>> {}
export type FactoryOrValue<T> = T | (() => T);
export interface MonoTypeOperatorFunction<T> extends OperatorFunction<T, T> {}
/**
* A value and the time at which it was emitted.
*
* Emitted by the `timestamp` operator
*
* @see {@link timestamp}
*/
export interface Timestamp<T> {
value: T;
/**
* The timestamp. By default, this is in epoch milliseconds.
* Could vary based on the timestamp provider passed to the operator.
*/
timestamp: number;
}
/**
* A value emitted and the amount of time since the last value was emitted.
*
* Emitted by the `timeInterval` operator.
*
* @see {@link timeInterval}
*/
export interface TimeInterval<T> {
value: T;
/**
* The amount of time between this value's emission and the previous value's emission.
* If this is the first emitted value, then it will be the amount of time since subscription
* started.
*/
interval: number;
}
/** SUBSCRIPTION INTERFACES */
export interface Unsubscribable {
unsubscribe(): void;
}
export type TeardownLogic = Subscription | Unsubscribable | (() => void) | void;
export interface SubscriptionLike extends Unsubscribable {
unsubscribe(): void;
readonly closed: boolean;
}
/** @deprecated To be removed in v8. Do not use. Most likely you want to use `ObservableInput` */
export type SubscribableOrPromise<T> = Subscribable<T> | Subscribable<never> | PromiseLike<T> | InteropObservable<T>;
/** OBSERVABLE INTERFACES */
export interface Subscribable<T> {
subscribe(observer: Partial<Observer<T>>): Unsubscribable;
}
/**
* Valid types that can be converted to observables.
*/
export type ObservableInput<T> = Observable<T> | InteropObservable<T> | AsyncIterable<T> | PromiseLike<T> | ArrayLike<T> | Iterable<T>;
/** @deprecated use {@link InteropObservable } */
export type ObservableLike<T> = InteropObservable<T>;
/**
* An object that implements the `Symbol.observable` interface.
*/
export interface InteropObservable<T> {
[Symbol.observable]: () => Subscribable<T>;
}
/** NOTIFICATIONS */
/**
* A notification representing a "next" from an observable.
* Can be used with {@link dematerialize}.
*/
export interface NextNotification<T> {
/** The kind of notification. Always "N" */
kind: 'N';
/** The value of the notification. */
value: T;
}
/**
* A notification representing an "error" from an observable.
* Can be used with {@link dematerialize}.
*/
export interface ErrorNotification {
/** The kind of notification. Always "E" */
kind: 'E';
error: any;
}
/**
* A notification representing a "completion" from an observable.
* Can be used with {@link dematerialize}.
*/
export interface CompleteNotification {
kind: 'C';
}
/**
* Valid observable notification types.
*/
export type ObservableNotification<T> = NextNotification<T> | ErrorNotification | CompleteNotification;
/** OBSERVER INTERFACES */
export interface NextObserver<T> {
closed?: boolean;
next: (value: T) => void;
error?: (err: any) => void;
complete?: () => void;
}
export interface ErrorObserver<T> {
closed?: boolean;
next?: (value: T) => void;
error: (err: any) => void;
complete?: () => void;
}
export interface CompletionObserver<T> {
closed?: boolean;
next?: (value: T) => void;
error?: (err: any) => void;
complete: () => void;
}
export type PartialObserver<T> = NextObserver<T> | ErrorObserver<T> | CompletionObserver<T>;
export interface Observer<T> {
next: (value: T) => void;
error: (err: any) => void;
complete: () => void;
}
export interface SubjectLike<T> extends Observer<T>, Subscribable<T> {}
/** SCHEDULER INTERFACES */
export interface SchedulerLike extends TimestampProvider {
schedule<T>(work: (this: SchedulerAction<T>, state?: T) => void, delay?: number, state?: T): Subscription;
}
export interface SchedulerAction<T> extends Subscription {
schedule(state?: T, delay?: number): Subscription;
}
/**
* This is a type that provides a method to allow RxJS to create a numeric timestamp
*/
export interface TimestampProvider {
/**
* Returns a timestamp as a number.
*
* This is used by types like `ReplaySubject` or operators like `timestamp` to calculate
* the amount of time passed between events.
*/
now(): number;
}
/**
* Extracts the type from an `ObservableInput<any>`. If you have
* `O extends ObservableInput<any>` and you pass in `Observable<number>`, or
* `Promise<number>`, etc, it will type as `number`.
*/
export type ObservedValueOf<O> = O extends ObservableInput<infer T> ? T : never;
/**
* Extracts a union of element types from an `ObservableInput<any>[]`.
* If you have `O extends ObservableInput<any>[]` and you pass in
* `Observable<string>[]` or `Promise<string>[]` you would get
* back a type of `string`.
* If you pass in `[Observable<string>, Observable<number>]` you would
* get back a type of `string | number`.
*/
export type ObservedValueUnionFromArray<X> = X extends Array<ObservableInput<infer T>> ? T : never;
/** @deprecated use {@link ObservedValueUnionFromArray} */
export type ObservedValuesFromArray<X> = ObservedValueUnionFromArray<X>;
/**
* Extracts a tuple of element types from an `ObservableInput<any>[]`.
* If you have `O extends ObservableInput<any>[]` and you pass in
* `[Observable<string>, Observable<number>]` you would get back a type
* of `[string, number]`.
*/
export type ObservedValueTupleFromArray<X> = { [K in keyof X]: ObservedValueOf<X[K]> };
/**
* Used to infer types from arguments to functions like {@link forkJoin}.
* So that you can have `forkJoin([Observable<A>, PromiseLike<B>]): Observable<[A, B]>`
* et al.
*/
export type ObservableInputTuple<T> = {
[K in keyof T]: ObservableInput<T[K]>;
};
/**
* Constructs a new tuple with the specified type at the head.
* If you declare `Cons<A, [B, C]>` you will get back `[A, B, C]`.
*/
export type Cons<X, Y extends readonly any[]> = ((arg: X, ...rest: Y) => any) extends (...args: infer U) => any ? U : never;
/**
* Extracts the head of a tuple.
* If you declare `Head<[A, B, C]>` you will get back `A`.
*/
export type Head<X extends readonly any[]> = ((...args: X) => any) extends (arg: infer U, ...rest: any[]) => any ? U : never;
/**
* Extracts the tail of a tuple.
* If you declare `Tail<[A, B, C]>` you will get back `[B, C]`.
*/
export type Tail<X extends readonly any[]> = ((...args: X) => any) extends (arg: any, ...rest: infer U) => any ? U : never;
/**
* Extracts the generic value from an Array type.
* If you have `T extends Array<any>`, and pass a `string[]` to it,
* `ValueFromArray<T>` will return the actual type of `string`.
*/
export type ValueFromArray<A extends readonly unknown[]> = A extends Array<infer T> ? T : never;
/**
* Gets the value type from an {@link ObservableNotification}, if possible.
*/
export type ValueFromNotification<T> = T extends { kind: 'N' | 'E' | 'C' }
? T extends NextNotification<any>
? T extends { value: infer V }
? V
: undefined
: never
: never;
/**
* A simple type to represent a gamut of "falsy" values... with a notable exception:
* `NaN` is "falsy" however, it is not and cannot be typed via TypeScript. See
* comments here: https://github.com/microsoft/TypeScript/issues/28682#issuecomment-707142417
*/
export type Falsy = null | undefined | false | 0 | -0 | 0n | '';
export type TruthyTypesOf<T> = T extends Falsy ? never : T;