2

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.

5
  • Your code compiles fine here, both using the command-line TypeScript compiler (inside an Angular-CLI project), and inside IntelliJ (which is WebStorm + a bunch of Java-related stuff). Version of TypeScript is 2.5.3. Does it compile fine out of WebStorm? Have you configured it to use the same TypeScript compiler as the one you're using on the command line? Commented Apr 1, 2018 at 16:51
  • 1
    We got used to WebStorm (IntelliJ ultimate) just sometimes not getting the clue and our codebase contains a lot of these tyoes of "errors". Sometimes it is "fixable", but sometimes it is not worth it. Commented Apr 1, 2018 at 16:56
  • @JBNizet I am also able to compile (via 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. Commented Apr 1, 2018 at 17:02
  • 1
    You should configure it to use the TypeScript version located in the node_modules of your project. That way, the IDE will display the same errors as the command-line compiler. Commented Apr 1, 2018 at 17:07
  • 2
    @JBNizet Thanks for that suggestion. WebStorm is configured (Languages & Frameworks > TypeScript) to use the typescript module 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. Commented Apr 1, 2018 at 17:19

2 Answers 2

5

Another workaround: open Settings -> Languages & Frameworks -> Typescript and make sure that Node interpreter value points to correct Node installation. See https://youtrack.jetbrains.com/issue/IDEA-278561

Sign up to request clarification or add additional context in comments.

Comments

0

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!

1 Comment

for me it's still causing issues..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.