Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 910
Hao Wu Avatar asked Sep 27 '26 23:09

Hao Wu


2 Answers

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;
like image 96
hackape Avatar answered Sep 29 '26 14:09

hackape


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'

like image 30
Chen Peleg Avatar answered Sep 29 '26 13:09

Chen Peleg