Specifying window (global) variable type hinting in VSCode from external JS file without typescript

Viewed 13

This may be a silly question but I really don't know where to look.

I'm creating a browser testing environment for a pretty large-scale API written in typescript. This API uses esbuild to build the typescript files into a /dist/ folder with a single index.js entry-point and its appropriate d.ts file.

I've created a /tests/ folder to hold some browser files that includes an index.html file with Mocha and Chai imported. It also imports /dist/index.js which is set globally to a window.myAPI variable.

In /tests/index.html:

import * as myAPI from "./dist/index.js"

Alongside index.html in the tests folder, there are separate JS files included for different tests that run things on window.myAPI... to do assertion tests.

search.test.js
book.test.js
navigate.test.js

I then run a server to host at the root. These separate tests are then imported from /tests/index.html. The separate tests look like this inside:

const { chai, mocha } = window;
const { assert } = chai;

describe("Search", function() {
    describe("Setup", function() {
        it("Setting URL should work", function() {
            const call = myAPI.someCall()
            assert.ok(call);
        });
    });
});

mocha.run();

Everything works, but I have no code hinting for myAPI. I'd like to be able to see what functions are available when I type myAPI, and what parameters they take, and what they should return - along with all my comments on each function.

In typescript you can do things like ambient declarations, but I don't want to make my tests typescript because then I add an unnecessary build step to the tests. But it would be as easy as:

/// <reference path = "/dist/index.d.ts" />

How can I tell VSCode that window.myAPI is an import of /dist/index.js and should import the types as well so I can see them ?

I'm open to different solutions to this, but I feel like this should be pretty simple. I don't know if ESLint is capable of doing something like this, but I tagged it because I feel it's relevant.

Thanks!

0 Answers
Related