为VSCode / Monaco Intellisence添加JavaScript类型提示 [英] Adding JavaScript type hints for VSCode/Monaco Intellisence

查看:392
本文介绍了为VSCode / Monaco Intellisence添加JavaScript类型提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法提示VSCode / Monaco的intellisense变量类型。



我有一些像这样的代码

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

我看到VSCode知道 loc 是a 网址





但它不知道 gl 是什么





也不知道 ctx





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



但它确实有的类型数据> WebGLRenderingContext





它知道 CanvasRenderingContext2D





有没有办法让我告诉vscode / monaco gl 是一个<$的实例c $ c> WebGLRenderingContext 而 ctx 是一个 CanvasRenderingContext2D 的实例,无需切换打字稿?也许通过添加某种评论?



我需要解决方案才能在




Is there a way to hint to VSCode/Monaco's intellisense the types of variables.

I have some code like this

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

I see that VSCode knows that loc is a URL

But it doesn't know what gl is

Nor does it know what ctx is

Which makes sense, having a function return different types based on its input is a somewhat unusual case.

But it does have type data for WebGLRenderingContext

and it knows CanvasRenderingContext2D

Is there a way to for me to tell vscode/monaco that gl is an instance of WebGLRenderingContext and that ctx is an instance of CanvasRenderingContext2D without having to switch to typescript? Maybe by adding some kind of comment?

I need the solution to work in monaco (which at least in my tests shows all the same completions) because this is for a WebGL tutorial site, not actually for VSCode but I'm hoping the solution is the same.

解决方案

Update: As of 0.9.0 of Monaco these type annotations now work


Is see that JSDoc style type annotations work in VSCode though they don't appear to work in Monaco.

var loc = window.location;

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

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

这篇关于为VSCode / Monaco Intellisence添加JavaScript类型提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆