JavaScript RxJS6 asObservable() needed on a Subject?

When is asObservable() needed on a Subject (e.g. BehaviorSubject) to get a observable of the subject? The subject isself can be casted to an Observable as well.

Questions

  1. What are the technical differences between name1$ and name2$?
  2. Which one should be used (name1$ or name2$)?

Code Sample

import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs';

export class Person {
  private nameSubject: BehaviorSubject<string> = new BehaviorSubject<string>('lorem');

  public get name1$(): Observable<string> {
    return this.nameSubject.asObservable();
  }

  public get name2$(): Observable<string> {
    return this.nameSubject;
  }

  public setName(value: string): void {
    this.nameSubject.next(value);
  }
}

Thank You for your answers in advance!

Answer:1

In general, it's recommended to use asObservable() only when using RxJS in JavaScript. With TypeScript you can use just type casting and it won't let you call next() and so on.

private nameSubject: BehaviorSubject<string> = new BehaviorSubject<string>('lorem');
public nameSubject$: Observable<string> = this.nameSubject;

This is the officially recommended way of turning Subjects to Observables in TypeScript.
For details see https://github.com/ReactiveX/rxjs/pull/2408#issuecomment-282077506.

Answer:2

There is not much difference, with asObservable() it mainly just block your access to observer methods - complete, next , error to prevent you from accidentally calling them

const a = new Subject();
console.log(a.asObservable().next)
// undefined

it is better to return asObservable() to be on the safe side if you not using that particular stream as observer

Answer:3

I wrote a gulp file that works but...not at any attempts. Indeed, I get this weird error 1 time / 3: [12:25:51] Starting 'default'... [12:25:51] Starting 'clean'... [12:25:51] Finished 'clean' ...

I wrote a gulp file that works but...not at any attempts. Indeed, I get this weird error 1 time / 3: [12:25:51] Starting 'default'... [12:25:51] Starting 'clean'... [12:25:51] Finished 'clean' ...

  1. gulp unhandled stream error in pipe
  2. gulp uglify unhandled stream error in pipe

I need to scroll to a form field (a displayfield actually) automatically. Since the field is dynamic, sometimes it is already shown in the rendered area of page, sometimes it is not. How can I do ...

I need to scroll to a form field (a displayfield actually) automatically. Since the field is dynamic, sometimes it is already shown in the rendered area of page, sometimes it is not. How can I do ...

I have read similar questions and no solutions seem to be working for this. I have a Angular material input box. The input should receive only numbers and a decimal dot. All other characters should be ...

I have read similar questions and no solutions seem to be working for this. I have a Angular material input box. The input should receive only numbers and a decimal dot. All other characters should be ...

  1. regular expression remove characters
  2. regular expression remove spaces
  3. regular expression remove blank lines
  4. regular expression remove punctuation
  5. regular expression remove whitespace
  6. regular expression remove numbers
  7. regular expression remove html tags
  8. regular expression remove line break
  9. regular expression remove characters from string
  10. regular expression remove everything after character
  11. regular expression remove non alphanumeric
  12. regular expression remove punctuation python
  13. regular expression remove non numeric
  14. regular expression remove spaces between words
  15. regular expression remove letters
  16. regular expression remove carriage return
  17. regular expression remove leading spaces
  18. regular expression remove last character
  19. regular expression remove all numbers
  20. regular expression remove trailing character

This is a clean version of the my situation: const person1 = { name: "Person1 Name", hairColor: "Brown", backpack: { color: "Army-Green", content: [ "item1", ...

This is a clean version of the my situation: const person1 = { name: "Person1 Name", hairColor: "Brown", backpack: { color: "Army-Green", content: [ "item1", ...