index.d.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * @license Angular v19.2.13
  3. * (c) 2010-2025 Google LLC. https://angular.io/
  4. * License: MIT
  5. */
  6. import { OutputOptions, OutputRef, DestroyRef, Signal, Injector, BaseResourceOptions, ResourceLoaderParams, ResourceRef } from '@angular/core';
  7. import { Observable, MonoTypeOperatorFunction, Subscribable } from 'rxjs';
  8. import { ValueEqualityFn } from '@angular/core/primitives/signals';
  9. /**
  10. * Declares an Angular output that is using an RxJS observable as a source
  11. * for events dispatched to parent subscribers.
  12. *
  13. * The behavior for an observable as source is defined as followed:
  14. * 1. New values are forwarded to the Angular output (next notifications).
  15. * 2. Errors notifications are not handled by Angular. You need to handle these manually.
  16. * For example by using `catchError`.
  17. * 3. Completion notifications stop the output from emitting new values.
  18. *
  19. * @usageNotes
  20. * Initialize an output in your directive by declaring a
  21. * class field and initializing it with the `outputFromObservable()` function.
  22. *
  23. * ```ts
  24. * @Directive({..})
  25. * export class MyDir {
  26. * nameChange$ = <some-observable>;
  27. * nameChange = outputFromObservable(this.nameChange$);
  28. * }
  29. * ```
  30. *
  31. * @publicApi
  32. */
  33. declare function outputFromObservable<T>(observable: Observable<T>, opts?: OutputOptions): OutputRef<T>;
  34. /**
  35. * Converts an Angular output declared via `output()` or `outputFromObservable()`
  36. * to an observable.
  37. *
  38. * You can subscribe to the output via `Observable.subscribe` then.
  39. *
  40. * @publicApi
  41. */
  42. declare function outputToObservable<T>(ref: OutputRef<T>): Observable<T>;
  43. /**
  44. * Operator which completes the Observable when the calling context (component, directive, service,
  45. * etc) is destroyed.
  46. *
  47. * @param destroyRef optionally, the `DestroyRef` representing the current context. This can be
  48. * passed explicitly to use `takeUntilDestroyed` outside of an [injection
  49. * context](guide/di/dependency-injection-context). Otherwise, the current `DestroyRef` is injected.
  50. *
  51. * @publicApi
  52. */
  53. declare function takeUntilDestroyed<T>(destroyRef?: DestroyRef): MonoTypeOperatorFunction<T>;
  54. /**
  55. * Options for `toObservable`.
  56. *
  57. * @developerPreview
  58. */
  59. interface ToObservableOptions {
  60. /**
  61. * The `Injector` to use when creating the underlying `effect` which watches the signal.
  62. *
  63. * If this isn't specified, the current [injection context](guide/di/dependency-injection-context)
  64. * will be used.
  65. */
  66. injector?: Injector;
  67. }
  68. /**
  69. * Exposes the value of an Angular `Signal` as an RxJS `Observable`.
  70. *
  71. * The signal's value will be propagated into the `Observable`'s subscribers using an `effect`.
  72. *
  73. * `toObservable` must be called in an injection context unless an injector is provided via options.
  74. *
  75. * @developerPreview
  76. */
  77. declare function toObservable<T>(source: Signal<T>, options?: ToObservableOptions): Observable<T>;
  78. declare function toObservableMicrotask<T>(source: Signal<T>, options?: ToObservableOptions): Observable<T>;
  79. /**
  80. * Options for `toSignal`.
  81. *
  82. * @publicApi
  83. */
  84. interface ToSignalOptions<T> {
  85. /**
  86. * Initial value for the signal produced by `toSignal`.
  87. *
  88. * This will be the value of the signal until the observable emits its first value.
  89. */
  90. initialValue?: unknown;
  91. /**
  92. * Whether to require that the observable emits synchronously when `toSignal` subscribes.
  93. *
  94. * If this is `true`, `toSignal` will assert that the observable produces a value immediately upon
  95. * subscription. Setting this option removes the need to either deal with `undefined` in the
  96. * signal type or provide an `initialValue`, at the cost of a runtime error if this requirement is
  97. * not met.
  98. */
  99. requireSync?: boolean;
  100. /**
  101. * `Injector` which will provide the `DestroyRef` used to clean up the Observable subscription.
  102. *
  103. * If this is not provided, a `DestroyRef` will be retrieved from the current [injection
  104. * context](guide/di/dependency-injection-context), unless manual cleanup is requested.
  105. */
  106. injector?: Injector;
  107. /**
  108. * Whether the subscription should be automatically cleaned up (via `DestroyRef`) when
  109. * `toSignal`'s creation context is destroyed.
  110. *
  111. * If manual cleanup is enabled, then `DestroyRef` is not used, and the subscription will persist
  112. * until the `Observable` itself completes.
  113. */
  114. manualCleanup?: boolean;
  115. /**
  116. * Whether `toSignal` should throw errors from the Observable error channel back to RxJS, where
  117. * they'll be processed as uncaught exceptions.
  118. *
  119. * In practice, this means that the signal returned by `toSignal` will keep returning the last
  120. * good value forever, as Observables which error produce no further values. This option emulates
  121. * the behavior of the `async` pipe.
  122. */
  123. rejectErrors?: boolean;
  124. /**
  125. * A comparison function which defines equality for values emitted by the observable.
  126. *
  127. * Equality comparisons are executed against the initial value if one is provided.
  128. */
  129. equal?: ValueEqualityFn<T>;
  130. }
  131. declare function toSignal<T>(source: Observable<T> | Subscribable<T>): Signal<T | undefined>;
  132. declare function toSignal<T>(source: Observable<T> | Subscribable<T>, options: NoInfer<ToSignalOptions<T | undefined>> & {
  133. initialValue?: undefined;
  134. requireSync?: false;
  135. }): Signal<T | undefined>;
  136. declare function toSignal<T>(source: Observable<T> | Subscribable<T>, options: NoInfer<ToSignalOptions<T | null>> & {
  137. initialValue?: null;
  138. requireSync?: false;
  139. }): Signal<T | null>;
  140. declare function toSignal<T>(source: Observable<T> | Subscribable<T>, options: NoInfer<ToSignalOptions<T>> & {
  141. initialValue?: undefined;
  142. requireSync: true;
  143. }): Signal<T>;
  144. declare function toSignal<T, const U extends T>(source: Observable<T> | Subscribable<T>, options: NoInfer<ToSignalOptions<T | U>> & {
  145. initialValue: U;
  146. requireSync?: false;
  147. }): Signal<T | U>;
  148. /**
  149. * Operator which makes the application unstable until the observable emits, completes, errors, or is unsubscribed.
  150. *
  151. * Use this operator in observables whose subscriptions are important for rendering and should be included in SSR serialization.
  152. *
  153. * @param injector The `Injector` to use during creation. If this is not provided, the current injection context will be used instead (via `inject`).
  154. *
  155. * @experimental
  156. */
  157. declare function pendingUntilEvent<T>(injector?: Injector): MonoTypeOperatorFunction<T>;
  158. /**
  159. * Like `ResourceOptions` but uses an RxJS-based `loader`.
  160. *
  161. * @experimental
  162. */
  163. interface RxResourceOptions<T, R> extends BaseResourceOptions<T, R> {
  164. loader: (params: ResourceLoaderParams<R>) => Observable<T>;
  165. }
  166. /**
  167. * Like `resource` but uses an RxJS based `loader` which maps the request to an `Observable` of the
  168. * resource's value.
  169. *
  170. * @experimental
  171. */
  172. declare function rxResource<T, R>(opts: RxResourceOptions<T, R> & {
  173. defaultValue: NoInfer<T>;
  174. }): ResourceRef<T>;
  175. /**
  176. * Like `resource` but uses an RxJS based `loader` which maps the request to an `Observable` of the
  177. * resource's value.
  178. *
  179. * @experimental
  180. */
  181. declare function rxResource<T, R>(opts: RxResourceOptions<T, R>): ResourceRef<T | undefined>;
  182. export { outputFromObservable, outputToObservable, pendingUntilEvent, rxResource, takeUntilDestroyed, toObservable, toSignal, toObservableMicrotask as ɵtoObservableMicrotask };
  183. export type { RxResourceOptions, ToObservableOptions, ToSignalOptions };