简体   繁体   中英

Sometimes Code Not Showing In Autocomplete List For Visual Studio Code, Solution?

In Visual Studio Code, I was making a simple WebGL app and found that sometimes the methods of my gl object would not appear in the autocomplete list like so:

pic1-不显示gl方法

When it is supposed to appear like this:

pic2-显示所有gl方法

I found that if I used document.getElementById(myCanvas) the autocomplete would not display gl methods like in pic1. However, if I used document.createElement('canvas') instead, the autocomplete would display all the methods from the gl object as seen in the second picture.

So does anyone know how to resolve the issue of not being see all my methods when not using document.createElement? As I'd very much like not being constrained by it.

note: this issue is not only limited to the webgl canvas, but also occurs when using the 2d canvas

Visual studio code has no way to know what kind of element document.getElementById is going to return. You can tell it the type of a variable though by adding a jsdoc style @type comment just before the line that declares the variable.

  /** @type {HTMLCanvasElement} */
  const canvas = document.getElementById("canvas");
  const gl = canvas.getContext("webgl");  

Now it should complete

在此处输入图片说明

note the two asterisks at the beginning of the comment are important.

/** @type {TheType} */    // good!
/* @type {TheType} */     // bad!

Also if you don't want to the it that canvas is an HTMLCanvasElement you can instead tell it that gl is a WebGLRenderingContext

  /** @type {WebGLRenderingContext} */
  const gl = canvas.getContext("webgl");  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM