You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio Code中对象解构时Intellisense无法正常工作

解决VS Code中对象解构时Intellisense类型识别为any的问题
  • 问题根源:你在JSDoc的@returns标注里使用了大写的String,这指向的是String构造函数的实例类型,而非JavaScript的原始字符串类型string。VS Code的Intellisense基于TypeScript实现,对这两者的处理逻辑不同,导致无法正确推断解构变量的类型。

  • 修正后的代码:

/**
 * @returns {{name: string, surname: string}}
 */
function Test(){
 return {name:"John",surname:"Doe"};
}
const {name,surname} = Test(); // 现在Intellisense会正确识别变量类型为string
  • 进阶优化(可选):如果需要更清晰的类型复用,可以先定义类型别名再引用:
/**
 * @typedef {Object} Person
 * @property {string} name - 姓名
 * @property {string} surname - 姓氏
 */

/**
 * @returns {Person}
 */
function Test(){
 return {name:"John",surname:"Doe"};
}
const {name,surname} = Test();

内容的提问来源于stack exchange,提问作者Kebs Studios

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 03:25:53