找到你要的答案

Q:Documenting Javascript code in VSCode for Intellisense

Q:记录在VSCode的IntelliSense JavaScript代码

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, i.e. the information that the parameter has to be a string and the returned value will be an ng.IHttpPromise?

我试图让我的JavaScript代码在Visual Studio代码正确感知的建议。特别是,我有以下angluarjs服务:

/// <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 );
        }
    };
} )

我想我应该使用XML文档注释,但他们似乎不工作,当我徘徊在hoverheretoseetype参数显示为“任何”(当返回值是正确推断使用角。d.ts)。所以问题的第一部分是:如何在函数中注释类型?

问题的第二部分是在实际使用服务时出现的:

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

我让IntelliSense不理解角的依赖注入,所以我需要对后端的类型。但我如何引用这种类型?

总之:我如何得到后端适当的IntelliSense。getcomments()叫第二段,即信息的参数是一个字符串,返回值将是一个ng.ihttppromise?

answer1: 回答1:

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;

这提供了悬停提示TypeScript。不完全是你想要的,但是如果他们把这个扩展到其他文件中,那就太好了。

/**
 * Change the role of the employee.
 * @param {number} x The id of the employee.
 */
tester: (x: number) => number = (x: number) => x * x;
answer2: 回答2:
  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.

  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.

  1. 型角对任何JavaScript文件的第一行在打开的文件夹。

  2. 注意这个词“角”是强调和一个灯泡出现。

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

注意一个新文件夹已出现在此引用。现在你将有智能感知。

javascript  angularjs  vscode  javascript-intellisense