【问题标题】:How to get intellisense for extracted variables?如何获得提取变量的智能感知?
【发布时间】:2020-01-17 18:21:57
【问题描述】:

问题

我试图通过将函数的参数提取为全局常量来重构 中的一些代码。但是,一旦我这样做了,我就无法在编辑该对象时使用

一些例子

我真的想要这个功能与我不熟悉的工具和库,但为了简单起见,我在下面做了一个简单的例子来演示我所指的内容。

智能感知示例

提取对象没有智能感知的示例

测试示例

/**
 * Example to test intellisense
 * @param {Object} person 
 * @param {string} person.first 
 * @param {string} person.last 
 */
function sayName(person) {
  console.log(person.first, person.last);
}

// Intellisense works here! ????
sayName({first: "Robert", last: "Todar"});

// Intellisense doesn't work here.. ☹
const person = {
  first: "Robert",
  last: "Todar"
}
sayName(person);

问题

当我从函数中提取参数时,有什么方法可以获得智能感知?

【问题讨论】:

    标签: visual-studio-code intellisense javascript visual-studio-code intellisense


    【解决方案1】:

    我相信您需要使用@typedef,它的作用类似于Global Definition,然后您需要在每个新对象的顶部添加@type@param |函数

    语法如下所示:

    /**
     * @typedef {object} person creates a new type 'object' named 'person'
     * @property {string} first - a 'string' property of 'person'
     * @property {string} last - a another 'string' property of 'person'
     * @property {number} [age] - an optional 'number' property of 'person'
     */
    

    对于 Functions 添加 /** @param {person} name */ 这将告诉 IntelliSense name 参数是指人对象 properties。 (@arg,@argument 也可以)

    /** @param {person} name */
    function sayName(name) {
        // IntelliSense works here
        console.log(name.first, name.last)
    }
    
    // and here too
    sayName({})
    


    至于 Objects: 添加 /** @type person */,这将告诉 IntelliSense john 对象与 person 对象相同。

    /** @type person */
    const john = {}
    


    【讨论】:

      猜你喜欢
      • 2011-08-25
      • 2016-08-22
      • 1970-01-01
      • 2021-11-23
      • 2011-01-25
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      • 2018-05-31
      相关资源
      最近更新 更多