Google Closure Compiler's @param annotation is used to document variable parameters in functions

I recently encountered an issue with a function that can accept a variable number of parameters.

After consulting the Google Closure Compiler wiki, I attempted to annotate the parameters using the @param annotation as suggested.

/**
 * Takes 2 or more strings and do something cool with them.
 * @param {...string} var_args
 * @return {string} the processed result
 */
function doSomethingCool() {
    var len = arguments.length;
    if (len < 2) {
        throw Error('Need at least 2 arguments');
    }

    ...
}

Issue

Upon compiling, a warning appeared stating:

JSC_INEXISTENT_PARAM: parameter var_args does not appear in doSomethingCool's parameter list at line 6 character 1

I tried using @param {string} arguments, but encountered the same error.

Even attempting @param {string} without specifying a variable name resulted in:

JSC_TYPE_PARSE_ERROR: Bad type annotation. expecting a variable name in a @param tag.

Inquiry

What went wrong in my annotation attempts and how should variable parameters be annotated for the Closure Compiler?

Answer №1

The error message is indicating that your var_args needs to be included in the parameter list.

/**
 * To do something cool with 2 or more strings.
 * @param {...string} var_args
 * @return {string} the processed result
 */
function doSomethingCool(var_args) {}

During compilation, Closure-compiler will recognize that it's not referenced and remove it.

If you prefer not to have it listed in the parameter list, you can use a @type annotation instead:

/**
 * To do something cool with 2 or more strings.
 * @type {function(...string):string}
 */
function doSomethingCool() {}

For accurate type checking, annotate the function to expect 2 or more strings:

/**
 * To do something unique with 2 or more strings.
 * @type {function(string, string, ...string):string}
 */
function doSomethingCool() {}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

preventing the ball from landing inside the container (JavaScript)

I developed a straightforward website with the following functionality: Upon entering any text into the prompt and clicking okay, a ball will drop into the 1st box, namely the Past Thoughts box. Below is the code snippet: HTML <h1> Welcome t ...

Tips for successfully transferring $scope to $factory

Hey there! I'm new to AngularJS and I'm trying to figure out how to pass my empty $scope to my $factory for dynamic login functionality. I'm using $http to retrieve data from my API and I attempted to assign the scope in the factory, but it ...

The React Material Component stubbornly resists being horizontally aligned in the Code Sandbox

Currently, I am working on getting my Material design to function properly within the CodeSandbox environment. One issue I am encountering is attempting to center it horizontally. As of now, it appears like this: https://i.sstatic.net/ZK02y.png To make ...

utilizing callback function for creating shopping cart feature within React

I'm in the process of creating an ecommerce website and implementing the add to cart functionality. I'm facing an issue where passing a callback function using props from a component to the parent component isn't working as expected. I' ...

Using scripted <svg> with <defs> and attempting to reference it via JavaScript results in failure

My goal is to dynamically generate svg path elements in html using JavaScript. I would like to place these paths within a <defs> element so that they can be reused later in <use> xlink:href elements. However, after creating the paths (by pr ...

confirm that the form is necessary and contains text

How can I ensure that a specific text string is validated for the input field labeled "promo"? Take a look at the code snippet below: <script> function validateForm() { var x = document.forms["myInquiry"]["promo"].value; if (x == null || x == "") ...

Utilize JavaScript to iterate through a JSON object and retrieve the indices that meet the specified criteria

While I found a previous answer that somewhat addresses my issue, I am seeking guidance on how to return an array of the indexes where a specific value appears. For example, if **18A38** is the target value, it should return the positions [1,3]. The sampl ...

A Nuxt plugin that integrates a separate website into the serverMiddleware

The concept Imagine having a main Nuxt website just like any other. Now, think about adding my module to your project. This module will then introduce a subdomain "admin.example.com" to your project, creating a fully functional Nuxt-based website that ope ...

Issue with submitting input fields that were dynamically added using jquery

I created a table where clicking on a td converts it into an input field with both name and value. The input fields are successfully generated, but they do not get submitted along with the form. In my HTML/PHP : <tr> <f ...

Error: The Tabs component is expecting a different `value`. The Tab with the current `value` ("0") is not present in the document structure

I am encountering an issue while using MUI tabs. The error message I receive is as follows: MUI: The value assigned to the Tabs component is not valid. The Tab with this value ("0") does not exist in the document layout. Please ensure that the tab item is ...

"Installing Snipcart with NextJS: The Perfect E-commerce Integration

It's really frustrating not being able to properly set up Snipcart. I've tried adding the script in different places, but it just throws errors. Can anyone help me figure out what I'm missing? I can add the link references in the 'Head ...

What is the best way to display a deeply nested array of unknown length without causing the browser to crash?

My data structure is quite complex and looks a bit like this: [{ name: 'name1', nodes: []}, { name: 'name2', nodes: [ { name: 'name21', nodes: [ { name: 'name211', nodes: []}, ...

How come an element retrieved with getElementById in Next.js comes back as null despite the presence of a defined document?

Having trouble using SSR in my React/Next app. Despite having the document present (and being able to visually see the div with the id plTable), the getElementById function is returning null. I even tried calling getElementById after 6 seconds to ensure ...

When trying to implement a dark/light theme, CSS variables may not function properly on the body tag

Currently, I am in the process of developing two themes (light and dark) for my React website. I have defined color variables for each theme in the main CSS file as shown below: #light{ --color-bg: #4e4f50; --color-bg-variant: #746c70; --color-primary: #e2 ...

Transfer properties to the children of this component

I'm a beginner with React and facing an issue when trying to pass custom props to this.props.children. I attempted using React.cloneElement, and while I can see the prop in the console.log within the class where I created it, it seems to get lost duri ...

Troubleshooting incompatibility issues between Tailwindcss and NextJS 12 due to experimental features

When I first started using NextJS 12, I decided to try building a project with tailwind. This is my package.json file: { "name": "test", "version": "0.1.0", "private": true, "scripts": { ...

Methods for removing cookie during logout with Express and Passport JS?

I have been struggling to delete cookies upon logout but haven't had any luck so far. I searched online and came across two methods: Setting a new expiration date for the cookie res.cookie('connect.sid', '', {expires: new Date(1 ...

Pros and cons of invoking a web API from the client-side

As I embark on creating a web application that integrates with multiple web services, I'd like to hear your thoughts on utilizing javascript to call these services. My Perspective: Advantages: - reduced bandwidth usage (server-side) - eliminates clie ...

Display the source code of an HTML element when clicked

Is there a way to show the source code of an element on a webpage in a text box when that specific element is clicked? I am wondering if it is feasible to retrieve the source code of an element upon clicking (utilizing the onClick property), and subseque ...

Tailored Filtering and Sorting Features for Material Table

In the table, there is a column labeled Active which contains data represented as 1 or 0 for active and inactive states. Instead of displaying the values as 1 or 0, a function called generateFlagText uses the render prop to show an MUI Chip component base ...