Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'BehaviorSubject<false>' is not assignable to type 'BehaviorSubject<boolean>'

I have this in one of my components:

public booleanSubject: BehaviorSubject<boolean> = new BehaviorSubject(false);

When I add "strictFunctionTypes": true to the tsconfig.json file I get the following error:

× Compiling TypeScript sources through NGC
ERROR: path/to/my/component.component.ts:28:10 - error TS2322: Type 'BehaviorSubject<false>' is not assignable to type 'BehaviorSubject<boolean>'.
  Types of property 'observers' are incompatible.
    Type 'Observer<false>[]' is not assignable to type 'Observer<boolean>[]'.
      Type 'Observer<false>' is not assignable to type 'Observer<boolean>'.
        Type 'boolean' is not assignable to type 'false'.

28   public booleanSubject: BehaviorSubject<boolean> = new BehaviorSubject(false);

Does anyone know the reason and how to get it not to throw the error by keeping the strictFunctionTypes flag set to true?

like image 210
Alavaros Avatar asked Mar 19 '21 15:03

Alavaros


1 Answers

Normally when I create a BehaviorSubject I just assign a type to it when initializing it. Try to it like this:

public booleanSubject = new BehaviorSubject<boolean>(false);

Doing so it will initialize the BehaviorSubject type as boolean and I believe the error will go away.

like image 154
Igor Melo Telheiro Avatar answered Oct 13 '22 18:10

Igor Melo Telheiro