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

Intellisense support in 'this' keyword inside an object. #9583

Closed
sant123 opened this issue Jul 9, 2016 · 3 comments
Closed

Intellisense support in 'this' keyword inside an object. #9583

sant123 opened this issue Jul 9, 2016 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@sant123
Copy link

sant123 commented Jul 9, 2016

Hi, maybe this is a bug but is not showing any intellisense regarding with the current object.

TypeScript Version: 1.8.0

Code

interface Person {
    name : string;
    lastName : string;
    age: number;

    greet() : string;
}


var People : Person[] = [{
    name : "Santi",
    lastName : "Aguilar",
    age : 21,
    greet : function(){
        return this.name;
    }
}]

Expected behavior:

Offer intellisense for the current object (this)

Actual behavior:

image

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jul 9, 2016

We experimented with inferring the type of this by the surrounding object, but because it broke a lot of existing code (see #8191 and #7801 (comment)), we couldn't add the change.

You'll be able to add an explicit this type in each method in TypeScript 2.0 (check our nightlies with npm install -g typescript@next):

var People : Person[] = [{
    name : "Santi",
    lastName : "Aguilar",
    age : 21,
    // Notice 'this: Person'
    greet : function(this: person) {
        return this.name;
    }
}]

Alternatively, you can add this: this to each method on the Person interface and implicitly have it work:

interface Person {
    name : string;
    lastName : string;
    age: number;

    greet(this: this) : string;
}


var People : Person[] = [{
    name : "Santi",
    lastName : "Aguilar",
    age : 21,
    greet : function() {
        return this.name;
    }
}]

If you ever want to avoid this being typed as any, we've also provided the --noImplicitThis flag, which gives an error when you used this and the type is not inferrable.

@DanielRosenwasser DanielRosenwasser added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jul 9, 2016
@sant123
Copy link
Author

sant123 commented Jul 10, 2016

Thank you for your answer @DanielRosenwasser 😄

@ahejlsberg
Copy link
Member

See #14141.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants