Alexander Mills
Alexander Mills

Reputation: 99970

globals declarations with TypeScript

Does the following style of declaration do anything in TypeScript files?

/* globals console define require React async */

or is there a different way to do this with TypeScript?

Upvotes: 1

Views: 461

Answers (1)

artem
artem

Reputation: 51579

No it has no effect on TypeScript compiler.

What globals are currently in scope is typically selected by choosing one of the predefined library settings in typescript compiler options, for example this command line

tsc --lib es5,dom

brings into global scope everything declared in files lib.es5.d.ts and lib.dom.d.ts which are supplied with the compiler.

If you look into lib.dom.d.ts you can see how for example window is declared there:

declare var window: Window;

Window is an interface defined earlier in that file.

You can do the same for your own global variables - you can create your own .d.ts file containing declare var at the top level, and include that file in compilation either on command line or adding it to files in tsconfig.json.

Everything you installed in node_modules/@types is also included in compilation by default, so every global declared by these typings is also in scope, unless you limit it by setting "typeRoots" or "types" in tsconfid.json.

Also, if you use some library with import, it will add all globals declared in that library type declaration file (and also in type declaration files from all its dependencies referenced with import or /// reference directive).

Upvotes: 3

Related Questions