Skip to content

The 'thisArg' parameter should be used to type 'this' in callbacks to map, filter, etc. #12548

Closed
@e-cloud

Description

@e-cloud

TypeScript Version: (2.2.0-dev.20161128)

Code

tets.ts

class A {
    options: any[]
    addOptions(options: any[]) {
        if (!this.options) {
            this.options = [];
        }
        options.forEach(function (item) {
            this.options.push(item);
        }, this);
        return this;
    }
}
tsc --module commonjs --noImplicitThis --noEmit test.ts

Expected behavior:
compile with no error output

Actual behavior:
test.ts(8,13): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.

As some built-in method of array can assign this, could it be inferred through the scopes?

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptFixedA PR has been merged for this issueHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions