Debian Codegraph @

node/rxjs/src/internal/types.ts

raw
1 // https://github.com/microsoft/TypeScript/issues/40462#issuecomment-689879308
2 /// <reference lib="esnext.asynciterable" />
3
4 import { Observable } from './Observable';
5 import { Subscription } from './Subscription';
6
7 /**
8 * Note: This will add Symbol.observable globally for all TypeScript users,
9 * however, we are no longer polyfilling Symbol.observable
10 */
11 declare global {
12 interface SymbolConstructor {
13 readonly observable: symbol;
14 }
15 }
16
17 /* OPERATOR INTERFACES */
18
19 /**
20 * A function type interface that describes a function that accepts one parameter `T`
21 * and returns another parameter `R`.
22 *
23 * Usually used to describe {@link OperatorFunction} - it always takes a single
24 * parameter (the source Observable) and returns another Observable.
25 */
26 export interface UnaryFunction<T, R> {
27 (source: T): R;
28 }
29
30 export interface OperatorFunction<T, R> extends UnaryFunction<Observable<T>, Observable<R>> {}
31
32 export type FactoryOrValue<T> = T | (() => T);
33
34 /**
35 * A function type interface that describes a function that accepts and returns a parameter of the same type.
36 *
37 * Used to describe {@link OperatorFunction} with the only one type: `OperatorFunction<T, T>`.
38 *
39 */
40 export interface MonoTypeOperatorFunction<T> extends OperatorFunction<T, T> {}
41
42 /**
43 * A value and the time at which it was emitted.
44 *
45 * Emitted by the `timestamp` operator
46 *
47 * @see {@link timestamp}
48 */
49 export interface Timestamp<T> {
50 value: T;
51 /**
52 * The timestamp. By default, this is in epoch milliseconds.
53 * Could vary based on the timestamp provider passed to the operator.
54 */
55 timestamp: number;
56 }
57
58 /**
59 * A value emitted and the amount of time since the last value was emitted.
60 *
61 * Emitted by the `timeInterval` operator.
62 *
63 * @see {@link timeInterval}
64 */
65 export interface TimeInterval<T> {
66 value: T;
67
68 /**
69 * The amount of time between this value's emission and the previous value's emission.
70 * If this is the first emitted value, then it will be the amount of time since subscription
71 * started.
72 */
73 interval: number;
74 }
75
76 /* SUBSCRIPTION INTERFACES */
77
78 export interface Unsubscribable {
79 unsubscribe(): void;
80 }
81
82 export type TeardownLogic = Subscription | Unsubscribable | (() => void) | void;
83
84 export interface SubscriptionLike extends Unsubscribable {
85 unsubscribe(): void;
86 readonly closed: boolean;
87 }
88
89 /**
90 * @deprecated Do not use. Most likely you want to use `ObservableInput`. Will be removed in v8.
91 */
92 export type SubscribableOrPromise<T> = Subscribable<T> | Subscribable<never> | PromiseLike<T> | InteropObservable<T>;
93
94 /** OBSERVABLE INTERFACES */
95
96 export interface Subscribable<T> {
97 subscribe(observer: Partial<Observer<T>>): Unsubscribable;
98 }
99
100 /**
101 * Valid types that can be converted to observables.
102 */
103 export type ObservableInput<T> =
104 | Observable<T>
105 | InteropObservable<T>
106 | AsyncIterable<T>
107 | PromiseLike<T>
108 | ArrayLike<T>
109 | Iterable<T>
110 | ReadableStreamLike<T>;
111
112 /**
113 * @deprecated Renamed to {@link InteropObservable }. Will be removed in v8.
114 */
115 export type ObservableLike<T> = InteropObservable<T>;
116
117 /**
118 * An object that implements the `Symbol.observable` interface.
119 */
120 export interface InteropObservable<T> {
121 [Symbol.observable]: () => Subscribable<T>;
122 }
123
124 /* NOTIFICATIONS */
125
126 /**
127 * A notification representing a "next" from an observable.
128 * Can be used with {@link dematerialize}.
129 */
130 export interface NextNotification<T> {
131 /** The kind of notification. Always "N" */
132 kind: 'N';
133 /** The value of the notification. */
134 value: T;
135 }
136
137 /**
138 * A notification representing an "error" from an observable.
139 * Can be used with {@link dematerialize}.
140 */
141 export interface ErrorNotification {
142 /** The kind of notification. Always "E" */
143 kind: 'E';
144 error: any;
145 }
146
147 /**
148 * A notification representing a "completion" from an observable.
149 * Can be used with {@link dematerialize}.
150 */
151 export interface CompleteNotification {
152 kind: 'C';
153 }
154
155 /**
156 * Valid observable notification types.
157 */
158 export type ObservableNotification<T> = NextNotification<T> | ErrorNotification | CompleteNotification;
159
160 /* OBSERVER INTERFACES */
161
162 export interface NextObserver<T> {
163 closed?: boolean;
164 next: (value: T) => void;
165 error?: (err: any) => void;
166 complete?: () => void;
167 }
168
169 export interface ErrorObserver<T> {
170 closed?: boolean;
171 next?: (value: T) => void;
172 error: (err: any) => void;
173 complete?: () => void;
174 }
175
176 export interface CompletionObserver<T> {
177 closed?: boolean;
178 next?: (value: T) => void;
179 error?: (err: any) => void;
180 complete: () => void;
181 }
182
183 export type PartialObserver<T> = NextObserver<T> | ErrorObserver<T> | CompletionObserver<T>;
184
185 /**
186 * An object interface that defines a set of callback functions a user can use to get
187 * notified of any set of {@link Observable}
188 * {@link guide/glossary-and-semantics#notification notification} events.
189 *
190 * For more info, please refer to {@link guide/observer this guide}.
191 */
192 export interface Observer<T> {
193 /**
194 * A callback function that gets called by the producer during the subscription when
195 * the producer "has" the `value`. It won't be called if `error` or `complete` callback
196 * functions have been called, nor after the consumer has unsubscribed.
197 *
198 * For more info, please refer to {@link guide/glossary-and-semantics#next this guide}.
199 */
200 next: (value: T) => void;
201 /**
202 * A callback function that gets called by the producer if and when it encountered a
203 * problem of any kind. The errored value will be provided through the `err` parameter.
204 * This callback can't be called more than one time, it can't be called if the
205 * `complete` callback function have been called previously, nor it can't be called if
206 * the consumer has unsubscribed.
207 *
208 * For more info, please refer to {@link guide/glossary-and-semantics#error this guide}.
209 */
210 error: (err: any) => void;
211 /**
212 * A callback function that gets called by the producer if and when it has no more
213 * values to provide (by calling `next` callback function). This means that no error
214 * has happened. This callback can't be called more than one time, it can't be called
215 * if the `error` callback function have been called previously, nor it can't be called
216 * if the consumer has unsubscribed.
217 *
218 * For more info, please refer to {@link guide/glossary-and-semantics#complete this guide}.
219 */
220 complete: () => void;
221 }
222
223 export interface SubjectLike<T> extends Observer<T>, Subscribable<T> {}
224
225 /* SCHEDULER INTERFACES */
226
227 export interface SchedulerLike extends TimestampProvider {
228 schedule<T>(work: (this: SchedulerAction<T>, state: T) => void, delay: number, state: T): Subscription;
229 schedule<T>(work: (this: SchedulerAction<T>, state?: T) => void, delay: number, state?: T): Subscription;
230 schedule<T>(work: (this: SchedulerAction<T>, state?: T) => void, delay?: number, state?: T): Subscription;
231 }
232
233 export interface SchedulerAction<T> extends Subscription {
234 schedule(state?: T, delay?: number): Subscription;
235 }
236
237 /**
238 * This is a type that provides a method to allow RxJS to create a numeric timestamp
239 */
240 export interface TimestampProvider {
241 /**
242 * Returns a timestamp as a number.
243 *
244 * This is used by types like `ReplaySubject` or operators like `timestamp` to calculate
245 * the amount of time passed between events.
246 */
247 now(): number;
248 }
249
250 /**
251 * Extracts the type from an `ObservableInput<any>`. If you have
252 * `O extends ObservableInput<any>` and you pass in `Observable<number>`, or
253 * `Promise<number>`, etc, it will type as `number`.
254 */
255 export type ObservedValueOf<O> = O extends ObservableInput<infer T> ? T : never;
256
257 /**
258 * Extracts a union of element types from an `ObservableInput<any>[]`.
259 * If you have `O extends ObservableInput<any>[]` and you pass in
260 * `Observable<string>[]` or `Promise<string>[]` you would get
261 * back a type of `string`.
262 * If you pass in `[Observable<string>, Observable<number>]` you would
263 * get back a type of `string | number`.
264 */
265 export type ObservedValueUnionFromArray<X> = X extends Array<ObservableInput<infer T>> ? T : never;
266
267 /**
268 * @deprecated Renamed to {@link ObservedValueUnionFromArray}. Will be removed in v8.
269 */
270 export type ObservedValuesFromArray<X> = ObservedValueUnionFromArray<X>;
271
272 /**
273 * Extracts a tuple of element types from an `ObservableInput<any>[]`.
274 * If you have `O extends ObservableInput<any>[]` and you pass in
275 * `[Observable<string>, Observable<number>]` you would get back a type
276 * of `[string, number]`.
277 */
278 export type ObservedValueTupleFromArray<X> = { [K in keyof X]: ObservedValueOf<X[K]> };
279
280 /**
281 * Used to infer types from arguments to functions like {@link forkJoin}.
282 * So that you can have `forkJoin([Observable<A>, PromiseLike<B>]): Observable<[A, B]>`
283 * et al.
284 */
285 export type ObservableInputTuple<T> = {
286 [K in keyof T]: ObservableInput<T[K]>;
287 };
288
289 /**
290 * Constructs a new tuple with the specified type at the head.
291 * If you declare `Cons<A, [B, C]>` you will get back `[A, B, C]`.
292 */
293 export type Cons<X, Y extends readonly any[]> = ((arg: X, ...rest: Y) => any) extends (...args: infer U) => any ? U : never;
294
295 /**
296 * Extracts the head of a tuple.
297 * If you declare `Head<[A, B, C]>` you will get back `A`.
298 */
299 export type Head<X extends readonly any[]> = ((...args: X) => any) extends (arg: infer U, ...rest: any[]) => any ? U : never;
300
301 /**
302 * Extracts the tail of a tuple.
303 * If you declare `Tail<[A, B, C]>` you will get back `[B, C]`.
304 */
305 export type Tail<X extends readonly any[]> = ((...args: X) => any) extends (arg: any, ...rest: infer U) => any ? U : never;
306
307 /**
308 * Extracts the generic value from an Array type.
309 * If you have `T extends Array<any>`, and pass a `string[]` to it,
310 * `ValueFromArray<T>` will return the actual type of `string`.
311 */
312 export type ValueFromArray<A extends readonly unknown[]> = A extends Array<infer T> ? T : never;
313
314 /**
315 * Gets the value type from an {@link ObservableNotification}, if possible.
316 */
317 export type ValueFromNotification<T> = T extends { kind: 'N' | 'E' | 'C' }
318 ? T extends NextNotification<any>
319 ? T extends { value: infer V }
320 ? V
321 : undefined
322 : never
323 : never;
324
325 /**
326 * A simple type to represent a gamut of "falsy" values... with a notable exception:
327 * `NaN` is "falsy" however, it is not and cannot be typed via TypeScript. See
328 * comments here: https://github.com/microsoft/TypeScript/issues/28682#issuecomment-707142417
329 */
330 export type Falsy = null | undefined | false | 0 | -0 | 0n | '';
331
332 export type TruthyTypesOf<T> = T extends Falsy ? never : T;
333
334 // We shouldn't rely on this type definition being available globally yet since it's
335 // not necessarily available in every TS environment.
336 interface ReadableStreamDefaultReaderLike<T> {
337 // HACK: As of TS 4.2.2, The provided types for the iterator results of a `ReadableStreamDefaultReader`
338 // are significantly different enough from `IteratorResult` as to cause compilation errors.
339 // The type at the time is `ReadableStreamDefaultReadResult`.
340 read(): PromiseLike<
341 | {
342 done: false;
343 value: T;
344 }
345 | { done: true; value?: undefined }
346 >;
347 releaseLock(): void;
348 }
349
350 /**
351 * The base signature RxJS will look for to identify and use
352 * a [ReadableStream](https://streams.spec.whatwg.org/#rs-class)
353 * as an {@link ObservableInput} source.
354 */
355 export interface ReadableStreamLike<T> {
356 getReader(): ReadableStreamDefaultReaderLike<T>;
357 }
358
359 /**
360 * An observable with a `connect` method that is used to create a subscription
361 * to an underlying source, connecting it with all consumers via a multicast.
362 */
363 export interface Connectable<T> extends Observable<T> {
364 /**
365 * (Idempotent) Calling this method will connect the underlying source observable to all subscribed consumers
366 * through an underlying {@link Subject}.
367 * @returns A subscription, that when unsubscribed, will "disconnect" the source from the connector subject,
368 * severing notifications to all consumers.
369 */
370 connect(): Subscription;
371 }