Issue
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.
Solution
This issue seems to have been fixed by an update of one or more components, as I no longer see any errors in the IDE. Move along, nothing to see!
- WebStorm 2019.3.4
- rxjs@6.6.3
- typescript@4.0.3
Answered By - Myk Willis
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.