I'm creating a BehaviorSubject inside a generic class in TypeScript like so:
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
interface Bar {
a: any;
}
class Foo<T extends Bar> {
subject = new BehaviorSubject<T[]>([]);
}
Here, subject is intended to emit arrays of T. However, the (WebStorm) compiler complains about the [] array passed to the BehaviorSubject constructor, saying:
Type argument cannot be inferred by usage
I get the same result even if I try to cast the array like:
subject = new BehaviorSubject<T[]>([] as T[]); // "Type argument cannot be inferred by usage"
What may be a clue is that if the type T does not extend any other type, there is no compiler complaint:
class Foo<T> {
subject = new BehaviorSubject<T[]>([]); // no warning here
}
Everything seems to function as expected at runtime, but I'm curious as to why I encounter this error.
ng build- this is an Angular project) without any issue. It appears this error is only being reported by WebStorm (2017.3.5). It claims to be using TypeScript 2.6.2.typescriptmodule installed in node_modules from my package.json, which I assume is the same one the CLI will use? I've also disabled tslint to be sure that's not the cause. WebStorm project configuration is a bit opaque to me, perhaps there is another setting hiding somewhere that is using a different background compiler.