如何让外部库智能感知在 javascript 的 vscode 中工作? [英] How to get external library intellisense to work in vscode for javascript?

查看:17
本文介绍了如何让外部库智能感知在 javascript 的 vscode 中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让智能感知为外部库(即 gsap)工作.

I'm trying to get intellisense to work for external libraries (namely gsap).

无论我怎么尝试,我都无法让它正常工作.

no matter what I try I can't get it to work properly.

/// <reference path="Tweenmax.min.js" />
//or
/// <reference type="gsap" />

添加三斜杠参考路径不会改变任何东西.我可以让它工作的唯一方法是导入这样的 ts 声明:

adding triple slash reference path doesn't change anything. the only way I could get it to work was importing a ts declaration like this:

import { TimelineMax } from "gsap";

我搜索了论坛和任何地方,有些人建议使用tsd",但它已被弃用并且不起作用,所以我使用了较新的 npm @types/gsap 希望它可以工作,但它只有在我导入时才有效.

I searched forums and everywhere, some suggested using "tsd" but it was deprecated and didn't work, so I used the newer npm @types/gsap hoping it would work, but it only works if I import it.

这在 vscode 中工作正常,因为它正确地显示了一个很好的自动完成功能,并且对界面有完美的理解.但是我不能像在浏览器中加载时那样使用此代码,chrome 不喜欢该行并引发如下错误:

This works fine in vscode as it properly shows a good autocomplete with a perfect understanding of the interface. but I can't use this code as in when it's loaded in the browser, chrome doesn't like that line and throws an error like this:

Uncaught SyntaxError: Unexpected token {

我已经为此工作了 2 个小时,但我无法接近一个好的解决方案.我记得以前有时 vscode 用于显示打开文档的自动完成功能,但即使这样也不起作用,即使我保持 TweenMax.min.js 打开,它仍然没有任何作用.

I've been working on this for 2 hours, and I can't get anywhere close to a good solution. I remember sometimes ago vscode used to show autocomplete for open documents, but even that doesn't work, even if I keep TweenMax.min.js open, it still does nothing.

我不知道我做错了什么.

I don't know what I'm doing wrong.

明确地说我不想使用打字稿文件然后编译它,我什至不想导入任何东西,我只想得到这样的东西:

to be clear I don't want to be using typescript file and then compiling it, I don't even want to import anything, I just want to get something like this:

到目前为止,我可以让它工作的唯一方法是在编码时保留导入声明,然后对其进行注释以进行测试.太蠢了,有什么好办法吗?

and the only way I could get it to work so far is to keep the import declaration while coding and then commenting it for testing. this is stupid, is there a good way?

推荐答案

不幸的是,远程加载定义 尚不支持.

Unfortunately, loading definitions remotely is not yet supported.

所以你有两个选择:

  • 安装依赖项(经典变体)
  • 安装@types

为了安装类型,您通常会这样做

In order to install types, you would typically do

  1. npm install @types/package-name
  2. ///

个人观察,@types依赖于node_modules物理存在.例如.我无法让它在 Yarn 2 PnP 中工作,因为它不使用 node_modules 文件夹.

Personal observations, @types rely on physical presence of node_modules. E.g. I could not get this to work this Yarn 2 PnP as it does not use node_modules folder.

我相信你有一个错误(错别字):

I believe you had an error (typo):

///<引用类型=gsap";/>

/// <reference type="gsap" />

应该输入s.

这篇关于如何让外部库智能感知在 javascript 的 vscode 中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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