Tips for inheriting method types from a parent class using JsDoc

I'm in need of writing JsDoc for an abstract method in the parent class so that it applies to all its sub-classes.

Here's a basic example similar to what I'm dealing with:

class Shape {
    /** Performs some complex functionality.
     * @param {number} myNumber a valid number.
     * @param {boolean} myBoolean a boolean flag.
     * @param {string} myString some string data.
     * @returns {string} important output.
     */
    doStuff(myNumber, myBoolean, myString) {}
}

The doStuff method isn't implemented as each subclass has its own implementation based on the same parameters and return type.

class Triangle extends Shape {
    doStuff(myNumber, myBoolean, myString) {
        return myBoolean ? myNumber.toString() : myString;
    }
}

In the provided screenshot, it's apparent that the text portion of the documentation is inherited, but the parameter types and return type are missing. How can I inherit the types correctly? I've tried using various tags such as @inheritdoc, @abstract, @extends, @override, and @link without success.

https://i.sstatic.net/jiZRy.png

Please keep in mind that this example is greatly simplified; in my actual code, the documentation block spans over 20 lines, and manually copying it to numerous subclasses isn't practical due to maintenance concerns.

Answer №1

Here is a potential solution that may not be flawless:

class Triangle extends Shape {
    /** @type {Shape['doStuff']} */
    doStuff(myValue, myCheck, myOutput) {
        return myCheck ? myValue.toString() : myOutput;
    }
}

Answer №2

Why not encapsulate your doStuff() function within its own @typedef declaration?

/**
* @typedef doStuff
* @type {Function}
* @param {Number} myNumber
* @param {Boolean} myBoolean
* @param {String} myString
* @returns {String}
*/

Then you can reference it in the extends section:

class Triangle extends Shape {
  /** @type {doStuff} */
  doStuff(){}
}

Answer №3

It seems unlikely that this can be achieved, given the limitations of JSDoc.

One could make a case for pulling parameter types from the original base class function instead.

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

retrieve JSON information using jQuery

Hi everyone, I'm trying to figure out how to retrieve JSON data using an AJAX call. I attempted the following method, but it doesn't seem to be working :P First off, I created a JavaScript file where I stored my JSON data: var food = [ ...

Error message occurred in Express/React application when Node was unable to locate the "error" view

My app was functioning properly a few hours ago, but now I am encountering an error upon starting it. The issue lies in the absence of a /views directory in /lib, which is present in my src/ directory, containing error.jade. Despite trying various soluti ...

The download functionality in HTML5 is not functioning properly, so users are forced to rename files when downloading

For weeks, I've been struggling to change the name of the downloaded file. Despite my efforts, instead of being named Chrysanthemum.jpg, it ends up as a hash of the file like 241693260.jpg In my backend setup, I utilize Node.js and Express.js for man ...

The significance of order when evaluating 2 Date Objects

While working with Date objects, I encountered something peculiar. When comparing two Date objects - let's call them a and b, the expressions a > b and b < a yield different results. Check out this JSFiddle for an example. var u = Date(2014,7, ...

Provide a numerical representation of how frequently one object value is found within another object value

Account Object Example in the Accounts Array: const accounts = [ { id: "5f446f2ecfaf0310387c9603", picture: "https://api.adorable.io/avatars/75/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e6b7d7a666 ...

Use the column name instead of the index with Jquery's eq() function

Looking for a way to use a static column name in jQuery to get the 5th column text in a table and change the text of a textbox $('textbox').val($(e.target).closest("tr").find('td:eq(4)').text()); If the index of the columns is changed ...

How to Utilize Multiple .includes in Vue.js Computed Properties?

I attempted to incorporate multiple .includes and .filters in my Vue.js computed function, but I have been unable to achieve success. HTML: <input class="searchbar" v-model="filterByName" placeholder="Search By Name" oni ...

Webpack converts 'import' statements to 'require'

I'm currently in the process of compiling my nodeJS project using webpack. Everything seems to be working correctly after compilation, but I've noticed that the imports are being changed to requires. This causes an error when trying to run index. ...

The issue of elements flickering while being hidden and shown with jQuery has been observed in both Chrome and

Having trouble with a simple animation using jQuery? While it may work smoothly in Firefox, you might be experiencing flickering in Chrome and Edge. Check out this jsfiddle for reference: HTML <div id="boxes-wrapper"> <div class="box"></ ...

The onChange function in React is not behaving as I anticipated

Consider the following data structure for tables: an array of objects containing nested objects: [ { "1": { "1": "Item s ", "2": "Manufacturer ", "3" ...

What is the best way to present both paragraphs and images from a div in a sequential order

Currently, I am developing a compact web tool to assist editors in creating content by utilizing buttons to insert paragraphs and images. The elements are saved with an ID (beginning at 0 and increasing for each new element) and can be previewed in a div n ...

Detecting the physical screen size based on the user agent in PHP and JavaScript

Is it possible to accurately detect the screen size of mobile browsers using both javascript and php? Given how mobile devices come in all shapes and sizes these days, relying solely on pixels may not be enough. I'm looking for a method that can speci ...

Troubleshooting undefined results with AngularJS ng-repeat filter

My objective is to create a Letter Filter, where users can click on buttons from A to Z to filter the displayed data. When clicking on the letter 'A' button, only data starting with 'A' should be shown. However, I have encountered an i ...

405 (Method Not Allowed) using the concept of notion

Currently, I am heavily involved in a compact project where data submitted through a form will be seamlessly posted on Notion. An error occurred while attempting to submit the form: contact.jsx?287f:14 POST http://localhost:3000/contact 405 (Meth ...

Transform a React functional component into vanilla JavaScript

I'm struggling to grasp the concept of how props are passed as arguments in a functional component using ES6 syntax in React. Is this a feature of ES6 or is it something specific to React that allows props to be magically pulled in as an argument with ...

Issue encountered while dealing with the compact CSS for the navigation bar on my current website project

Seeking assistance with adjusting the CSS of a website's navbar. Looking for help to align items to the right and ensure responsiveness. HTML: <nav> <a href="#" class="logo"><img src="./images/logo.png& ...

display rails view using ajax

I have developed a new form that I would like to render using ajax in case it fails validations. In my controller, the code looks like this: def create event = CEvent.new(params[:c_event]) respond_to do |format| if event.save format.html { ...

Creating a unique function to map an array in VueJS specifically designed for table manipulation

I am currently working on displaying and sorting data in a bootstrap table within VueJS. My goal is to change the date format within an array retrieved from an API endpoint. The original date format is in "January 21, 2010" and I need it to be in "MM/DD/Y ...

Upgrade your yarn version in order to rectify any yarn audit errors

Currently, there doesn't seem to be a yarn audit --fix command available. Thus, I am exploring different approaches to resolve the errors detected in my yarn audit. While executing a yarn upgrade, some of the errors were successfully addressed, but a ...

eval concealing program in JGraph compared to obfuscation and packing

Many times when the topic of eval is brought up, the response is always the same - avoid using eval. However, I believe there is a valid reason for eval to exist. Nevertheless, there are numerous pitfalls to consider. Regarding jgraph - why do they incorp ...