简体   繁体   中英

Documenting Javascript code in VSCode for Intellisense

I'm trying to get proper Intellisense suggestions for my javascript code in Visual Studio Code. In particular, I have the following AngluarJS service:

/// <reference path="definitelytyped/angularjs/angular.d.ts" />
var module = angular.module( 'testApp', [] );
module.factory( 'backend', function ( $http ) {
    return {
        "getComments": function HoverHereToSeeType( post ) {
            /// <summary>Retrieves comments from the backend</summary>
            /// <param name="post" type="string">Post to retrieve comments for</param>
            return $http.get( "/rest/" + post );
        }
    };
} )

I thought I should be using XML Documentation Comments , but they don't seem to work - when I hover over HoverHereToSeeType the parameter is shown as "any" (while the return value is properly inferred using angular.d.ts). So the first part of the question is: How do I annotate types in my functions?

The second part of the question comes up when actually trying to use the service:

module.controller( 'MyCtrl', function( backend ) {
    backend.getComments( "test" );
} );

I get that IntelliSense doesn't understand Angular's dependency injection, so I'll need to annotate backend 's type. But how do I reference that type?

In short: How do I get proper Intellisense for the backend.getComments() call in the second snippet, ie the information that the parameter has to be a string and the returned value will be an ng.IHttpPromise?

This provides hover hints in TypeScript. Not exactly what you want, but if they extend this to show them in other files, it would be great.

/**
 * Change the role of the employee.
 * @param {number} x The id of the employee.
 */
tester: (x: number) => number = (x: number) => x * x;
  1. Type angular on the first line of any javascript file in your open folder.

  2. Notice the word "angular" is underlined and a lightbulb appears.

添加对Angular的引用

  1. You have the option of adding a reference to angular. Do this.

Notice a new folder has appeared with this reference in it. You will now have intellisense.

打字文件夹

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM