The tsc
compiler always happily compiles console.log("foo")
. Where is the variable console
declared? The compiler accepts this program even with all lib
s 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.)
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.
"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 @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 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 © 2022 - 2024 — McMap. All rights reserved.
console.log
in your IDE, it should take you to the file where those are declared :) – Sprinkling