Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot infer from usage when generic class does not have a member or function parameter/return value of the generic type. #10609

Closed
stephanedr opened this issue Aug 30, 2016 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@stephanedr
Copy link

TypeScript Version: 2.1.0-dev.20160830

Code

interface A<T> {
    test(value: Object): value is T;
    // a: T;  // No error if uncommented.
    // or test(value: T | Object): value is T;
}

type B<T> = T | A<T>;

function f1<T>(b: B<T>) {}

function f2<T>(b: B<T>) {
    f1(b); // The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
           //   Type argument candidate 'T' is not a valid type argument because it is not a supertype of candidate 'T'.
}

Expected behavior:
No error.

Actual behavior:
The error reported with the code.

Note that this may seem same as #8619, however here, adding a member of type T, or a function with a parameter/return value of type T, solves the issue.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 30, 2016

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Aug 30, 2016
@stephanedr
Copy link
Author

But there is reference to T in "value is T".

@stephanedr
Copy link
Author

Unless TS considers "value is T" as just a boolean?

@mhegazy
Copy link
Contributor

mhegazy commented Aug 30, 2016

There is not really a "use" of T that the compile can infer from. i.e. a parameter or a property. The main issue here is TS type system is structural, it tries to use the structure to know the type, these are things that would exist at runtime in your js code.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants