Skip to content

Type inference error with generics and function 'this' typing #9673

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

Closed
use-strict opened this issue Jul 13, 2016 · 3 comments · Fixed by #9746
Closed

Type inference error with generics and function 'this' typing #9673

use-strict opened this issue Jul 13, 2016 · 3 comments · Fixed by #9746
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@use-strict
Copy link

TypeScript Version: [email protected]

Code

interface JQuery {
    each<T>(
        collection: T[],
        callback: (this: T, indexInArray: number, valueOfElement: T) => any
    ): any;
}

let $: JQuery;
let list: string[];
$.each(list, function() {
    return this != 'abc';
});

Expected behavior:
No errors
Actual behavior:
Operator '!=' cannot be applied to types 'T' and 'string'.

Notes:

  • Explicit 'string' type parameter fixes the error: $.each<string>(...), although the type is correctly inferred to string even without it.
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jul 13, 2016
@sandersn
Copy link
Member

Also, replacing return this != 'abc' with return this.indexOf('x') != 0 works. It seems related specifically to the binary operator with this.

@mhegazy mhegazy added this to the TypeScript 2.0.1 milestone Jul 13, 2016
@sandersn
Copy link
Member

Well, that's only VS Code apparently. Now that I'm running a test case from the command line, this.indexOf('x') fails with the same error.

@sandersn
Copy link
Member

A fix is up at #9746, although I need some expert help to make sure it's the correct fix (it's hacky, so it's probably not correct yet).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants