Hao Wu
Hao Wu

Reputation: 20669

Global type declaration in vscode for javascript

I have a /typings/$app.d.ts looks like this.

declare class App {
    test: object
}

export const $app: App;

enter image description here

But in order to use the intellisense, I have to auto-import it and it will generate a line like this on the first line of my javascript code.

import { $app } from "../../typings/$app";

It will get in some error since it's a d.ts file.

Is there a way to make this $app.d.ts global like how the window does?

enter image description here

Upvotes: 2

Views: 3239

Answers (2)

Chen Peleg
Chen Peleg

Reputation: 2034

I would like to make a comment on @hackape 's answer and also recommend (if you haven't already) to add a tsconfig.json file to your project's root. Note that you can also add tsconfig if you don't want to use any typescript (only for intellisense).

You can have a minimal file like this:

{
  "compilerOptions": {
    "allowJs": true,
    "typeRoots": ["./typings"],
    "outFile": "./justANameToMakeTSHappy.js"
  }
}

allowJs - so vscode would know to use the types also in the JS files. typeRoots - let ts know where your types are. outFile - so you don't get the error of 'can't overwrite file ****.js'

Upvotes: 1

hackape
hackape

Reputation: 19957

declare var something in .d.ts file top level scope is the keyword to expose global variable. Don't do export if you don't want to export anything.

declare class App {
    test: object
}

declare var $app: App;

Upvotes: 2

Related Questions