Jshint / PhpStorm: “Unresolved variable” when usin

2019-05-28 14:47发布

Phpstorm keeps telling me I have an undefined variable input.connectto

Html: <div class="b-showColorinList" data-connectto="123456" data-othervalue="Lorem Ipsum">...

JS:

$(document).on('click', '.b-showColorinList', function() {
    cm.showColorInList( $(this) );
});

And:

/**
 * Uses ajax to get other color in list view
 * @param {object} inputObj
 */
cm.showColorInList = function(inputObj) {
"use strict";

var input = inputObj.data(),
    parent = $("#"+input.connectto),
    othervalue = input.othervalue;

I know I can ignore a line in jshint, but is there any way to make it correct with jsdoc, example define input as an object

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-05-28 15:33

Accordingly to JSDoc docs the proper way should using @typedef to define actual object structure (especially useful if it will be re-used later in another place) and @type to declare type of particular variable:

/**
 * @typedef {Object} MyInputData
 * @property {string} connectto 
 * @property {string} othervalue
 */

/** @type {MyInputData} */
var input = inputObj.data();

This one (with just @typedef and variable name as type name) seems to work in PhpStorm as well:

/**
 * @typedef {Object} input
 * @property {string} connectto 
 * @property {string} othervalue
 */
var input = inputObj.data();
查看更多
登录 后发表回答