简体   繁体   中英

How to get external library intellisense to work in vscode for javascript?

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" />

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";

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.

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 {

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.

So you have two options here:

  • install dependency (classic variant)
  • install @types

In order to install types, you would typically do

  1. npm install @types/package-name
  2. /// <reference types="package-name" />

Personal observations, @types rely on physical presence of node_modules . Eg 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):

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

It should be type s instead.

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