Sampath
Sampath

Reputation: 65940

Member should be declared before all private instance

private userSubject$: BehaviorSubject<UserModel> = new BehaviorSubject<UserModel>(null);
userChanged$: Observable<UserModel> = this.userSubject$.asObservable();

It says:

Member userChanged$ should be declared before all private instance field definitions.eslint@typescript-eslint/member-ordering

I like this rule. But how can I use the above code without disabling this rule?

If I'll change the order then it says:

Property 'userSubject$' is used before its initialization.

Upvotes: 5

Views: 11905

Answers (2)

mor222
mor222

Reputation: 410

You can also use "get" for the observable:

private _userSubject: BehaviorSubject<UserModel> = new BehaviorSubject<UserModel>(null);

get getUserSubject$(): Observable<UserModel> {
    return this._userSubject.asObservable();
}

Upvotes: 0

MrCodingB
MrCodingB

Reputation: 2314

You can move the initialization into the constructor:

userChanged$: Observable<UserModel>;

private userSubject$: BehaviorSubject<UserModel>;

constructor() {
    this.userSubject$ = new BehaviorSubject<UserModel>(null);
    this.userChanged$ = this.userSubject$.asObservable();
}

Upvotes: 8

Related Questions