gpt4 book ai didi

javascript - 为 VSCode/Monaco Intellisense 添加 JavaScript 类型提示

转载 作者:搜寻专家 更新时间:2023-11-01 04:49:58 25 4
gpt4 key购买 nike

有没有办法向 VSCode/Monaco 的智能感知提示变量的类型。

我有这样的代码

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 glWebGLRenderingContext 的一个实例并且 ctx 是一个实例CanvasRenderingContext2D 无需切换到 typescript ?也许通过添加某种评论?

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

最佳答案

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


看到JSDoc style type annotations在 VSCode 中工作,尽管它们似乎在 Monaco 中不起作用。

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 - 为 VSCode/Monaco Intellisense 添加 JavaScript 类型提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39694407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com