Explaining function parameter properties with JSDoc in WebStorm

How can I declare that company is a String in this code snippet using JSDoc?

fetch('https://api.github.com/users/mdo').then(res => res.json()).then(res => {
  console.log(res.company);
});

When I use WebStorm, it shows company as an Unresolved variable:

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

If I add

/** @namespace bogus.company **/

anywhere in the file, WebStorm accepts it, but it seems odd:

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

Is this a bug in WebStorm, or am I misunderstanding how JSDoc declarations should be used?

Should JSDoc even be used for this scenario?

Answer №1

If you go through the inspection regarding unresolved javascript variable, there is an option called

Report undeclared properties as error
that you have the choice to deselect.

Personally, I tend to follow this approach:

/**
 * @param {Object} res
 * @property {String} company
 */
function myOutput(res) {
  console.log(res.company);
}

fetch('https://api.github.com/users/mdo')
  .then(myToJson)
  .then(myOutput);

However, for small anonymous functions, it may not be worth bothering about. Since it's a minor warning, you could either disable the inspection or simply tolerate it. While it is feasible to include /** @param {*} something */ in anonymous functions, it often ends up reducing readability.

Another alternative is creating a dummy file like something.js within your content-root with an anonymous function defined as follows:

(() => {
  return {
    "github-users-response" : {
      "company" : "sample"
    }
  }
});

By doing so, WebStorm will recognize that 'company' is indeed defined in your other files. You can even place it in a separate content-root if you prefer to keep it isolated from your main project.

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

Creating a unique Angular 2 Custom Pipe tutorial

I've come across various instances of NG2 pipes online and decided to create one myself recently: @Pipe({name: 'planDatePipe'}) export class PlanDatePipe implements PipeTransform { transform(value: string): string { return sessionStor ...

Using v-model to dynamically update the router based on certain conditions

Check out this demo showcasing a navigation menu with nested items. Clicking "more" reveals the expanded list, which can be set to always open using v-model="item.model" with model = true. My goal is to have the submenu stay open only when the user is on ...

Conduct unit testing on the React component created by a function that takes an array of objects as a parameter

Looking to test a function that returns a React component using enzyme. Component const overrideTimeoutValues = [ {minutes: 0, seconds: 10}, {minutes: 0, seconds: 15}, {minutes: 0, seconds: 20}, {minutes: 0, seconds: 30}, {minutes: 1, ...

Is the number 9933272057275866 truly magical?

I encountered a strange issue that I cannot quite understand. It has left me quite baffled. When I attempt to increase the number 9933272057275866 by 1, it somehow adds 2 instead!!! Here is the code snippet: let test = 9933272057275866; let test2 = test+1 ...

Implement an AJAX function to prompt a save dialog before initiating the download process

I'm currently programming an embedded device in C with a web server. One of the tasks I am working on is downloading files from this device. I need to download several files at once, so I've set up an AJAX request that uses a POST method and send ...

Error message: "An issue occurred with the Bootstrap Modal in

I've designed an AngularJS app like so: <!DOCTYPE html> <html ng-app="StudentProgram"> <head> <title>Manage Student Programs</title> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2. ...

How to implement a form in PHP that doesn't refresh the page upon submission

I am having an issue with this ajax code. I know it works, but for some reason it's not submitting. <script type="text/javascript"> $(function(){ $('input[type=submit]').click(function(){ $.ajax({ type: "POST", ...

Utilizing the document.createDocumentFragment() method along with innerHTML for manipulating a Document Object Model (

I am currently in the process of creating a document fragment using the code below: let fullHTMLDocument = '<!doctype html> <html><head></head><body><h1>hello world</h1></body></html>'; let f ...

Transferring a variable from a .jsp file to a .js file

I'm currently working on a .jsp page where the appearance of a different div from a .js file depends on the header that comes from a request. I've added a second header and now want if (info.getLightHeader()!=null) the <div id="lightHeader ...

Whenever I create the code using javascript, padding seems to always show up

I have a basic stacked row layout that displays an upper box and lower box without any padding. <span id="route" class="small"> <div class="row" style="border-style:solid;"> <div class="col-md-12 col-12"> <p>UpperBox</p& ...

Is Redux necessary for arranging ItemList on the left side and ItemContent on the right side?

Is Redux necessary for managing the app state in order to achieve this goal? If not, what is the best way to handle routes and state for <ItemList /> and <ItemContent >/ components? Would it be beneficial to create a <ItemsListAndContent / ...

No value is stored in the Javascript variable

https://i.sstatic.net/iE5N5.png I am currently using an xml file as a database for my website, with a table that displays all the products. Adding a product to the xml automatically updates the table (refer to the image). Now, I want to implement the func ...

Enforce a limit of two decimal places on input fields using jQuery

Looking to limit an input field so that users can only enter numbers with a maximum of two decimals. Is it possible to achieve this using jQuery? Any way I could utilize the jQuery toFixed() function for this purpose? Thank you in advance! ...

Simultaneous fading and displaying of the navbar

Currently, I'm in the process of constructing a navigation bar using Bootstrap v4 and have encountered an issue with the collapsing animation in responsive view. Upon inspection of the specific navigation bar, I noticed that the classes (.in and .s ...

Developing front-end libraries using the jspm workflow

What is the best way to convert a library written in TypeScript to ES5? While JSPM documentation focuses on web apps (such as with jspm bundle-sfx), the information I've found on Google seems more suited for a web app workflow rather than a library w ...

executing an SQL query with mysql-npm on the Amazon Web Services platform

Hey everyone, I'm facing a bit of a challenge that I'm struggling to solve. It's kind of a strange issue :/ I've created a Lambda function to connect to a MySQL DB using the 'mysql' node package. When I run the function from ...

How can AngularJS utilize variables from an external JavaScript <script> file within an HTML document?

As someone unfamiliar with AngularJS, I have a simple inquiry regarding the subject. The code on my page begins by defining an app and controller: <script> var isisApp = angular.module('isisApp', []); isisApp.controller('Acco ...

transpile TypeScript code into ES6 rather than ES5

I am currently diving into typescript and I have observed that the code output always defaults to using var. Is there a way to make it output const and let in the .js file instead, or does typescript always require es5 output for some reason? Thanks. Her ...

Dynamically import a file from a specific URL

Are you looking for a way to dynamically load file content from an external source during runtime? const MyComponent = (props) => { const [dynamicComponent, fetchComponent] = React.useState(null); const loadComponentDynamically = ()=> { ...

Exploring Inner Textures on a Cylinder in Three.js

I am brand new to Three.js and am currently using it for a small feature on my website. My goal is to create a 3D model of a paper cup, with different textures on the outer and inner sides. So far, I have been able to achieve some rotation functionality ...