编程语言
首页 > 编程语言> > 为VSCode / Monaco Intellisence添加JavaScript类型提示

为VSCode / Monaco Intellisence添加JavaScript类型提示

作者:互联网

有没有办法暗示VSCode / Monaco的intellisense变量的类型.

我有一些像这样的代码

var loc = window.location;
var gl = context1.getContext("webgl");
var ctx = context2.getContext("2d");

我看到VSCode知道loc是一个URL

vscode knows loc

但它不知道gl是什么

vscode does not know gl

它也不知道ctx是什么

vscode does not know ctx

这是有道理的,让函数根据其输入返回不同的类型是一个有点不寻常的情况.

但它确实有WebGLRenderingContext的类型数据

vscode knows webglrenderingcontext

它知道CanvasRenderingContext2D

vscode knows canvasrenderingcontext2d

有没有办法让我告诉vscode / monaco gl是WebGLRenderingContext的一个实例,ctx是CanvasRenderingContext2D的一个实例,而不必切换到typescript?也许通过添加某种评论?

我需要解决方案在monaco中工作(至少在我的测试中显示所有相同的完成),因为这是针对WebGL教程站点,实际上不是VSCode,但我希望解决方案是相同的.

解决方法:

更新:从摩纳哥的0.9.0开始,这些类型的注释现在可以使用了

看到JSDoc style type annotations在VSCode中工作,虽然它们似乎不适用于摩纳哥.

var loc = window.location;

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

/** @type {CanvasRenderingContext2D} */
var ctx = context2.getContext("2d"); 

enter image description here

enter image description here

标签:javascript,visual-studio-code,intellisense,monaco-editor
来源: https://codeday.me/bug/20191008/1871215.html