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
?
Igor Melo Telheiro answer is on point. Just to extend it a bit.
You can either declare that field with public booleanSubject = new BehaviorSubject(false);
and it will automatically infer the right type BehaviorSubject<boolean>
Or you can declare the field with a type but then the assigned value must be also of that type when declared.
public booleanSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false)
;