简体   繁体   English

Visual Studio Code JavaScript Intellisense无法用于对象属性

[英]Visual Studio Code JavaScript Intellisense not working for objects properties

I'm using Visual Studio Code 1.11.1. 我正在使用Visual Studio Code 1.11.1。

For the following code, Intellisense works correctly, understanding that the canvas variable is of type HTMLCanvasElement : 对于以下代码,Intellisense可以正常工作,要了解canvas变量的类型为HTMLCanvasElement

var canvas = document.getElementsByTagName('canvas')[0];

In fact, when I write the name of that variable followed by a dot it shows me all properties and methods of HTMLCanvasElement . 实际上,当我写该变量的名称后跟一个点时,它向我展示了HTMLCanvasElement的所有属性和方法。

However, using the following code, in which I wrap that variable inside an object (acting as a namespace), Intellisense doesn't understand anymore that the variable is of type HTMLCanvasElement : 但是,使用下面的代码,其中我将该变量包装在一个对象中(充当名称空间),Intellisense不再理解该变量的类型为HTMLCanvasElement

// create a namespace "App"
var App;
App = {};

App.canvas = document.getElementsByTagName('canvas');

When I write " App.canvas " followed by a dot character, Intellisense doesn't show me all the properties and methods of HTMLCanvasElement . 当我写“ App.canvas ”后跟一个点字符时,Intellisense不会向我显示HTMLCanvasElement的所有属性和方法。 In fact, it considers App.canvas of type any . 实际上,它认为类型为any的 App.canvas

I've tried also to use the @type annotation like in the following code, but the result is the same: 我也尝试过使用@type批注,如以下代码所示,但结果是相同的:

// create a namespace "App"
var App;
App = {};

/** @type {HTMLCanvasElement} */
App.canvas = document.getElementsByTagName('canvas')[0];

Is it possible to make Intellisense understand the variable types for properties of objects (like App.canvas in my example)? 是否可以使Intellisense了解对象属性的变量类型(例如在我的示例中为App.canvas )?

I work on JS/TS support for VSCode. 我致力于VSCode的JS / TS支持。 We use the TypeScript project to power both our JS and TS language support, so believe you are running into this bug: https://github.com/Microsoft/TypeScript/issues/10868 我们使用TypeScript项目来支持我们的JS和TS语言支持,因此相信您会遇到此错误: https : //github.com/Microsoft/TypeScript/issues/10868

As a workaround, try declaring the type of canvas on App itself: 解决方法是,尝试在App本身上声明canvas的类型:

/** @type {{canvas:HTMLCanvasElement}} */
var App;
App = {};

App.canvas = document.getElementsByTagName('canvas')[0];

Using an object literal should also work: 使用对象文字也应该起作用:

var App = { canvas: document.getElementsByTagName('canvas')[0] }

We're looking into improving IntelliSense in these cases 在这种情况下,我们正在研究改进IntelliSense

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio Code JavaScript Intellisense无法在嵌入式对象上运行 - Visual Studio Code JavaScript Intellisense not working on inline objects Visual Studio Code Intellisense 不适用于 Javascript - Visual Studio Code Intellisense not working for Javascript Visual Studio 代码智能感知在 javascript 文件中的自动完成 function 调用中不起作用 - visual studio code intellisense not working in autocompleted function call in javascript files JavaScript 的 Visual Studio Code 自动完成/IntelliSense 无法正常工作 - Visual Studio Code autocomplete/IntelliSense not working properly for JavaScript Visual Studio代码Intellisense方法定义Javascript - Visual Studio Code Intellisense Method Definitions Javascript Visual Studio Code Intellisense Javascript导入 - Visual Studio Code Intellisense Javascript import Intellisense无法在Visual Studio Code中使用导入 - Intellisense not working with imports in Visual Studio Code Visual Studio Code Intellisense和JavaScript ES5 - Visual Studio Code Intellisense and JavaScript ES5 Visual Studio 2012 JavaScript智能感知不起作用 - Visual Studio 2012 JavaScript Intellisense Not Working Javascript Visual Studio Intellisense不能在一个项目上工作 - Javascript Visual Studio Intellisense Not Working On One Project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM