eaf4c95d353c0bf41bf87a24f00668249bb74430dda54ed0c339bc5995718b0ab2d225967cd3ae8cfd3b57afda54d4d0b90203486b34cb2b0a34a0ec6dd9e6 12 KB

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