• Overview
@angular/core/rxjs-interop

toSignal

function

Get the current value of an Observable as a reactive Signal.

API

  
    
  
  
function toSignal<T>(source: Observable<T> | Subscribable<T>): Signal<T | undefined>;
@paramsourceObservable<T> | Subscribable<T>
@returnsSignal<T | undefined>
function toSignal<T>(source: Observable<T> | Subscribable<T>, options: NoInfer<ToSignalOptions<T | undefined>> & { initialValue?: undefined; requireSync?: false | undefined; }): Signal<T | undefined>;
@paramsourceObservable<T> | Subscribable<T>
@paramoptionsNoInfer<ToSignalOptions<T | undefined>> & { initialValue?: undefined; requireSync?: false | undefined; }
@returnsSignal<T | undefined>
function toSignal<T>(source: Observable<T> | Subscribable<T>, options: NoInfer<ToSignalOptions<T | null>> & { initialValue?: null | undefined; requireSync?: false | undefined; }): Signal<T | null>;
@paramsourceObservable<T> | Subscribable<T>
@paramoptionsNoInfer<ToSignalOptions<T | null>> & { initialValue?: null | undefined; requireSync?: false | undefined; }
@returnsSignal<T | null>
function toSignal<T>(source: Observable<T> | Subscribable<T>, options: NoInfer<ToSignalOptions<T>> & { initialValue?: undefined; requireSync: true; }): Signal<T>;
@paramsourceObservable<T> | Subscribable<T>
@paramoptionsNoInfer<ToSignalOptions<T>> & { initialValue?: undefined; requireSync: true; }
@returnsSignal<T>
function toSignal<T, U>(source: Observable<T> | Subscribable<T>, options: NoInfer<ToSignalOptions<T | U>> & { initialValue: U; requireSync?: false | undefined; }): Signal<T | U>;
@paramsourceObservable<T> | Subscribable<T>
@paramoptionsNoInfer<ToSignalOptions<T | U>> & { initialValue: U; requireSync?: false | undefined; }
@returnsSignal<T | U>

Description

Get the current value of an Observable as a reactive Signal.

toSignal returns a Signal which provides synchronous reactive access to values produced by the given Observable, by subscribing to that Observable. The returned Signal will always have the most recent value emitted by the subscription, and will throw an error if the Observable errors.

With requireSync set to true, toSignal will assert that the Observable produces a value immediately upon subscription. No initialValue is needed in this case, and the returned signal does not include an undefined type.

By default, the subscription will be automatically cleaned up when the current injection context is destroyed. For example, when toSignal is called during the construction of a component, the subscription will be cleaned up when the component is destroyed. If an injection context is not available, an explicit Injector can be passed instead.

If the subscription should persist until the Observable itself completes, the manualCleanup option can be specified instead, which disables the automatic subscription teardown. No injection context is needed in this configuration as well.

Jump to details