简体   繁体   English

如何使用 Google Closure Compiler 注释可选参数?

[英]How to annotate an optional parameter using Google Closure Compiler?

I am very new to Google Closure Compiler (GCC).我对 Google Closure Compiler (GCC) 非常陌生。 I am confused on how to make it show a parameter as optional...我对如何使它显示为可选参数感到困惑......

Here is part of my code:这是我的代码的一部分:

/**
 * @param {string} name The event name
 * @param {Date} date1 The start date (If alone, the single date) of the event
 * @param {Date} date2 The end date of the event
 */
function getEventLink (name,date1,date2) {
    // code here
}

I want to have date1 be optional... I found a helpful page on the Closure Compiler , but I didn't see and option for optional... is it possible?我想让date1是可选的...我在 Closure Compiler 上找到了一个有用的页面,但我没有看到可选的选项...可能吗? If so, how would I do it?如果是这样,我会怎么做?

I've tried doing:我试过做:

/**
 * @param {string} name The event name
 * @param {Date} date1 The start date (If alone, the single date) of the event
 * @param {Date|undefined} date2 The end date of the event
 */
function getEventLink (name,date1,date2) {
    // code here
}

Also, with null instead of undefined , but neither seemed to work...此外,使用null而不是undefined ,但似乎都不起作用......

The Google Closure Compiler relies on annotations to do its work. Google Closure Compiler 依靠注解来完成它的工作。 Because JavaScript has no syntax for types these have to be written as comments in the source files.因为 JavaScript 没有类型的语法,所以这些必须作为注释写在源文件中。

These comments are written using JSDoc and even though GCC has come with its own tags over the years (eg @polymer isn't a "native" JSDoc tag), it does support JSDoc types expressions.这些注释是使用 JSDoc 编写的,尽管 GCC 多年来都有自己的标签(例如@polymer不是“原生”JSDoc 标签),但它确实支持 JSDoc 类型表达式。 (And JSDoc does support the Google Closure Type System in its type expressions too.) (而且 JSDoc 在其类型表达式中也支持 Google 闭包类型系统。)

Annotation?注解? Expression?表达? What?!什么?!

A simple example will clarify:一个简单的例子将阐明:

/**
 * @param {string|number} x
 */
const a = x => x;

This is the annotation: @param {string|number} x .这是注释: @param {string|number} x It is information about a thing:它是关于事物的信息:

  1. it is a parameter它是一个参数
  2. its name is x它的名字是x
  3. its type is either a string or a number它的类型是字符串或数字

This is the type expression: {string|number} .这是类型表达式: {string|number} It is information about the type of a thing.它是关于事物类型的信息。

Optional parameters in JavaScript JavaScript中的可选参数

As you know JavaScript allows to specify default values for parameters in the function signature:如您所知 JavaScript 允许在 function 签名中指定参数的默认值:

const a = (x=1, y=2, z=3) => x + y + z;

These default values are used if and only if undefined is passed either implicitly or explicitly:当且仅当undefined被隐式或显式传递时,才使用这些默认值:

a();
//=> 6

a(undefined, undefined, 20);
//=> 23

a(null, null, 20);
//=> 20 (null doesn't trigger the default value and is coerced to 0)

Optional parameters in JSDoc JSDoc 中的可选参数

There are two annotations possible for an optional parameter.可选参数有两种可能的注释。 Just pick the one you like:随便挑一个你喜欢的:

/** @param {number=} x */

or或者

/** @param {number} [x] */

However there is only one for an optional parameter with a default value:但是,只有一个具有默认值的可选参数:

/** @param {number} [x=1] */

Optional parameters with GCC可选参数与 GCC

Even though a(undefined, undefined, 20) is technically possible in JavaScript, it is however a poor developer experience and GCC may complain.尽管a(undefined, undefined, 20)在 JavaScript 中在技术上是可行的,但是它的开发人员体验很差,GCC可能会抱怨。

Optional parameters without declared default values in the function signature should be last otherwise GCC will issue a warning. function 签名中没有声明默认值的可选参数应该放在最后,否则 GCC 将发出警告。

In this example z is the only non-optional parameter however it is the last parameter:在此示例中, z是唯一的非可选参数,但它是最后一个参数:

/**
 * @param {number} [x=1]
 * @param {number} [y=2]
 * @param {number} z
 */
const a = (x, y, z) => (x ?? 1) + (y ?? 2) + z;

a(undefined, undefined, 20);
//=> 23

GCC output: GCC output:

JSC_OPTIONAL_ARG_AT_END: optional arguments must be at the end at line 11 character 10
const a = (x, y, z) => (x ?? 1) + (y ?? 2) + z;
          ^

This is of course not an issue if all parameters are optional.如果所有参数都是可选的,这当然不是问题。 These examples are all OK:这些例子都OK:

/**
 * @param {number} [x=1]
 * @param {number} [y=2]
 * @param {number} [z=3]
 */
const a = (x, y, z=3) => (x ?? 1) + (y ?? 2) + z;

or或者

/**
 * @param {number} [x=1]
 * @param {number} [y=2]
 * @param {number} [z=3]
 */
const a = (x, y, z) => (x ?? 1) + (y ?? 2) + (z ?? 3);

or或者

/**
 * @param {number} [x=1]
 * @param {number} [y=2]
 * @param {number} [z=3]
 */
const a = (x=1, y=2, z=3) => x + y + z;

Further readings进一步阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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