Where does TypeScript find its variable declarations?
Asked Answered
G

1

7

The tsc compiler always happily compiles console.log("foo"). Where is the variable console declared? The compiler accepts this program even with all libs turned off in my tsconfig.json. So is console assumed to be universal? What other variables are always declared? More generally, how can I find out what other declarations exist, and where they are coming from? (The only debugging flag I can find is --extendedDiagnostics, but it seems almost useless.)

Granulocyte answered 3/10, 2020 at 15:15 Comment(1)
When you do Command + Click or Ctrl + Click on console.log in your IDE, it should take you to the file where those are declared :)Sprinkling
T
8

TypeScript is distributed with a set of lib.*.d.ts declaration files which describe the standard library APIs provided by various JavaScript runtimes.

console is described therein.

The language determines which of these files are applicable based on the --target option, automatically including them in the compilation context.

For example "target": "es5" will cause the compiler to include lib.es5.d.ts in the compilation context.

You can explicitly configure which of these built-in declaration files should be referenced using the --lib option.

For example "lib": ["es2015", "dom", "dom.iterable"].

If you use the navigation features of your IDE, such "go to definition" in Visual Studio Code, will navigate to the to the decoration for console, allowing you to see where it is.

Additional declaration files, such as those provided by various @types packages, may also contribute potentially overlapping declarations describing JavaScript runtime APIs. This is actually good behavior because it allows packages to describe additional capabilities they provide.

For example @types/node contains such decorations.

When declared by 3rd party packages, the inclusion of these additional global declaration files is controlled by the --types option as opposed to the --lib option.

For example "types": ["node"].

The inclusion of these types is not impacted by the --target option.

Township answered 3/10, 2020 at 15:15 Comment(4)
Thanks - though my confusion is that even when I set "lib": [], the compiler still seems to know about console, leading me to assume that console is also declared somewhere else. My expectation is that when setting "lib": [], there should be no predeclared variables at all, but the existence of console violates that.Granulocyte
Yes. There's no restriction that other declaration files won't Define some subset of the standard Library types. This is actually good behavior because it allows packages to describe additional capabilities of their run time. For example @types/node contains such decorations. The global types added to the when declared by 3rd party packages are controlled by the "types" option instead of "lib" Township
You're right - in my case console was declared in @types/node in C:\Users\james\AppData\Local\Microsoft\TypeScript\4.0. I don't understand what the compiler's search path is for typings - but that's a future question. Thanks!Granulocyte
That's actually a feature called Automatic Type Aquisition it can be disabledTownship

© 2022 - 2024 — McMap. All rights reserved.