When declaring a class with a member like:
import {Subject} from "rxjs";
export class MyClass {
protected subject: Subject<string>;
}
what is the preferred practice in TypeScript for initializing the subject
member? Is it better to do it in the constructor like:
export class MyClass {
protected subject: Subject<string>;
constructor() {
this.subject = new Subject<string>();
}
}
or directly inline within the class body like:
export class MyClass {
protected subject: Subject<string> = new Subject<string>();
}
NOTE
Inline initialization works with imported classes like Subject
in this case, but not with injected classes which are typically initialized in the constructor.
EDIT
The Angular Style Guide does not specifically address this topic.