Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does TypeScript find its variable declarations?

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.)

like image 462
jameshfisher Avatar asked Oct 03 '20 15:10

jameshfisher


People also ask

Where are variables declared?

A scope is a region of the program and broadly speaking there are three places, where variables can be declared: Inside a function or a block which is called local variables, In the definition of function parameters which is called formal parameters. Outside of all functions which is called global variables.

Where do I put declaration files TypeScript?

Most of the libraries are bundled with a type declaration file of their own in their package distribution, which means once we install a package, we will get all type declaration files inside node_modules/<package-name>/lib . TypeScript uses this and does its magic.

How does TypeScript find type definitions?

When loading a JavaScript module from “node_modules” TypeScript will look in “node_modules/@types” to see if there is a corresponding type definition for this file. That's where the “@types” scope is special.


1 Answers

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.

like image 94
4 revs Avatar answered Sep 24 '22 08:09

4 revs