Where is the syntax for TypeScript comments docume

2019-01-13 20:09发布

问题:

Is the syntax for TypeScript comments documented anywhere?

And by any chance, does it now support the C# /// system?

回答1:

TypeScript uses JSDoc. e.g.

/** This is a description of the foo function. */
function foo() {
}

To learn jsdoc : http://usejsdoc.org/

But you don't need to use the type annotation extensions in JSDoc.



回答2:

You can add information about parameters, returns, etc. as well using:

/**
* This is the foo function
* @param bar This is the bar parameter
* @returns returns a string version of bar
*/
function foo(bar: number): string {
    return bar.toString()
}

This will cause editors like VS Code to display it as the following:



回答3:

You can use comments like in regular JavaScript:

TypeScript syntax is a superset of Ecmascript 5 (ES5) syntax. [...]

This document describes the syntactic grammar added by TypeScript

Other than that, I only found this about comments in the language specs:

TypeScript also provides to JavaScript programmers a system of optional type annotations. These type annotations are like the JSDoc comments found in the Closure system, but in TypeScript they are integrated directly into the language syntax. This integration makes the code more readable and reduces the maintenance cost of synchronizing type annotations with their corresponding variables.

11.1.1 Source Files Dependencies:

A comment of the form /// <reference path="..."/> adds a dependency on the source file specified in the path argument. The path is resolved relative to the directory of the containing source file

Source:
https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md



回答4:

The accepted answer is no longer valid.

The right answer is now to use the syntax used by TSDoc. It will allow you to have your comments understood by Visual Studio Code, or other documentation tools.

A good overview of the syntax is available here and especially here. The precise spec should be soon written up.

Note: you should not use JSDoc, as explained on TSDoc main page: Why can't JSDoc be the standard? Unfortunately the JSDoc grammar is not rigorously specified, but rather inferred from the behavior of a particular implementation. The majority of the standard JSDoc tags are preoccupied with providing type annotations for plain JavaScript, which is an irrelevant concern for a strongly-typed language such as TypeScript. TSDoc addresses these limitations while also tackling a more sophisticated set of goals.



回答5:

TypeScript is a strict syntactical superset of JavaScript hence

  • Single line comments start with //
  • Multi-line comments start with /* and end with */