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

Go to definition cannot work as expected #15218

Closed
davisduan opened this issue Apr 17, 2017 · 9 comments
Closed

Go to definition cannot work as expected #15218

davisduan opened this issue Apr 17, 2017 · 9 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@davisduan
Copy link

Here is the case:

var x = {
    "abc": function () {
        return 1;
    },
    "def": function () {
        this.abc(); //cannot go to definition
    }
};

This function works well in sublime

@mhegazy
Copy link
Contributor

mhegazy commented Apr 17, 2017

The issue is not go-to-def. the issue is that the type of this is any in an object literal by default. the rational here is object literals are passed around as property bags, and can be accessed with different this arguments.

If you are running with TS 2.3 or later, we have a change only under --noImplicitThis that allows for a stricter type of this in object literals among other things. go to def should work as expected under this flag.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Apr 17, 2017
@davisduan
Copy link
Author

davisduan commented Apr 18, 2017

@mhegazy Thanks for your explanation. It works after I added the parameter. But I meet the same problem when I put the object as parameter:

Parent.extend("Child", {
    "abc": function () {
        return 1;
    },
    "def": function () {
        this.abc(); //cannot go to definition - the type of this is any
    }
});

@mhegazy
Copy link
Contributor

mhegazy commented Apr 18, 2017

how is Parent.extend defined?

@davisduan
Copy link
Author

I don't think it's quite related to the function definition. But you can take jQuery extend function as reference.

jQuery.extend({
    "abc": function () {
        return 1;
    },
    "def": function () {
        this.abc(); //cannot go to definition - the type of this is any
    }
});

@mhegazy
Copy link
Contributor

mhegazy commented Apr 19, 2017

JQuery.extend is defined with target : any; this pushes a contextual type of any for the function, and thus the this parameter.

if it was defined using generics instead:

declare function extend<T>(target: T): any;

you would get your this type correctly under --noImplicitThis.

You can take this one step further and make the type of this the combination of all the object literal properties using ThisType<T>, so:

declare function extend<T, U, V>(target: T & ThisType<T & U & V>, object1: U & ThisType<T & U & V>, object2: V & ThisType<T & U & V>): any;

extend({
        "abc": function () {
            return 1;
        },
        "def": function () {
            this.abc(); // OK
        }
    },
    {
        "another": function () {
            this.def(); // OK
         }
    },
    {
        "yetAnother": function () {
            this.another(); // OK
         }
    });

you can find more info about the use of ThisType in #14141

@davisduan
Copy link
Author

Actually, I just want to use Visual Studio Code to develop in JavaScript, which is is using TypeScript as JS compiler. But I cannot use "declare function ..." in JS code.
Seems it too complex for me to achieve the "Go to definition" function works like sublime.
Anyway, thank you very much. @mhegazy

@mhegazy
Copy link
Contributor

mhegazy commented Apr 20, 2017

Where is extends comming from? VSCode gets your declarations form an @types package, so updating it should do the trick without you having to change your code.

@davisduan
Copy link
Author

Extends is one of functions in my JS library. Here is only a example. I don't think I can add all the functions declaration manually.

@mhegazy
Copy link
Contributor

mhegazy commented May 8, 2017

This issue seems to have been already addressed or is unactionable at the moment. I am auto-closing it for now.

@mhegazy mhegazy closed this as completed May 8, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

2 participants