Skip to content

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

Closed
@use-strict

Description

@use-strict

TypeScript Version: typescript@2.1.0-dev.20160713

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.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions