哪里记录了 TypeScript 注释的语法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23072286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where is the syntax for TypeScript comments documented?
提问by David Thielen
Is the syntax for TypeScript comments documented anywhere?
是否在任何地方记录了 TypeScript 注释的语法?
And by any chance, does it now support the C# ///
system?
无论如何,它现在是否支持 C#///
系统?
采纳答案by Qortex
The right syntax is now the one used by TSDoc. It will allow you to have your comments understood by Visual Studio Code or other documentation tools.
正确的语法现在是TSDoc使用的语法。它将允许您让 Visual Studio Code 或其他文档工具理解您的评论。
A good overview of the syntax is available hereand especially here. The precise spec should be "soon" written up.
语法的很好的概述,请点击这里和这里特别。精确的规范应该“很快”写出来。
Another file worth checking out is this onewhere you will see useful standard tags.
另一个值得检查的文件是这个文件,您将在其中看到有用的标准标签。
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.
注意:您不应该使用 JSDoc,如 TSDoc 主页所述:为什么 JSDoc 不能成为标准?不幸的是,JSDoc 语法没有严格规定,而是从特定实现的行为中推断出来的。大多数标准 JSDoc 标签都专注于为纯 JavaScript 提供类型注释,这与 TypeScript 等强类型语言无关。TSDoc 解决了这些限制,同时还解决了一组更复杂的目标。
回答by basarat
TypeScript uses JSDoc. e.g.
TypeScript 使用 JSDoc。例如
/** This is a description of the foo function. */
function foo() {
}
To learn jsdoc : https://jsdoc.app/
学习jsdoc:https://jsdoc.app/
But you don't need to use the type annotation extensions in JSDoc.
但是您不需要使用 JSDoc 中的类型注释扩展。
You can (and should) still use other jsdoc block tagslike @returns
etc.
您可以(并且应该)仍然使用其他 jsdoc块标记,例如@returns
等。
Example
例子
Just an example. Focus on the types (not the content).
只是一个例子。关注类型(而不是内容)。
JSDoc version (notice types in docs):
JSDoc 版本(文档中的通知类型):
/**
* Returns the sum of a and b
* @param {number} a
* @param {number} b
* @returns {number}
*/
function sum(a, b) {
return a + b;
}
TypeScript version (notice the re-location of types):
TypeScript 版本(注意类型的重新定位):
/**
* Takes two numbers and returns their sum
* @param a first input to sum
* @param b second input to sum
* @returns sum of a and b
*/
function sum(a: number, b: number): number {
return a + b;
}
回答by Sharpiro
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:
这将导致像 VS Code 这样的编辑器将其显示如下:
回答by CodeManX
You can use comments like in regular JavaScript:
您可以像在常规 JavaScript 中一样使用注释:
TypeScript syntax is a superset of Ecmascript 5 (ES5) syntax. [...]
This document describes the syntactic grammar added by TypeScript
TypeScript 语法是 Ecmascript 5 (ES5) 语法的超集。[...]
本文档描述了 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.
TypeScript 还为 JavaScript 程序员提供了一个可选的类型注释系统。这些类型注解类似于 Closure 系统中的 JSDoc 注释,但在 TypeScript 中它们直接集成到语言语法中。这种集成使代码更具可读性,并降低了将类型注释与其相应变量同步的维护成本。
11.1.1 Source Files Dependencies:
11.1.1 源文件依赖:
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
表单的注释
/// <reference path="..."/>
添加了对路径参数中指定的源文件的依赖。路径是相对于包含源文件的目录解析的
Source:
https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md
来源:https:
//github.com/Microsoft/TypeScript/blob/master/doc/spec.md
回答by Ayushi Jain
TypeScript is a strict syntactical superset of JavaScript hence
TypeScript 是 JavaScript 的严格语法超集,因此
- Single line comments start with //
- Multi-line comments start with /* and end with */
- 单行注释以 // 开头
- 多行注释以 /* 开头,以 */ 结尾