Skip to content

Strange this parameter behaviour #9648

Closed
@olmobrutall

Description

@olmobrutall

TypeScript Version: 2.0.0 beta with "noImplicitThis": true

Code

interface String {
     contains(this: string, str: string): boolean;
}

 // OK
String.prototype.contains = function (this: string, str: string): boolean { 
    return this.indexOf(element) != -1;
};

//Error: 'this' implicitly has type 'any' because it does not have a type annotation.   
String.prototype.contains = function (str: string) {
    return this.indexOf(str) != -1;
};

// OK
String.prototype.contains = function (str): boolean { 
    return this.indexOf(str) != -1;
};

// OK
String.prototype.contains = function (str) { 
    return this.indexOf(str) != -1;
};

Look like once you type some parameters you have to type all of them, including the this.

What's the reason?

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions