Alexander Nechay Alexander Nechay - 2 months ago 125
TypeScript Question

What does my TypeScript compiler compile?

I have the following tsconfig.json file

{
"compilerOptions": {
"noImplicitAny": true,
"target": "es5"
}
}


And single TypeScript file src/app/pages/details/testts.ts:

let x = 5;


When I run tsc from the folder where tsconfig.json placed I get this diagnostics:

D:\Workspaces\MyProject\client>tsc src/app/pages/details/testts.ts --diagnostics
Files: 2
Lines: 19005
Nodes: 95663
Identifiers: 35490
Symbols: 94717
Types: 12063
Memory used: 92569K
I/O read: 0.00s
I/O write: 0.00s
Parse time: 0.35s
Bind time: 0.17s
Check time: 1.58s
Emit time: 0.03s
Total time: 2.12s


It says I have compiled two files with 19005 lines in total for 2.12s!
I believe I have one file and one line in it. What has went wrong?

Answer

The second file is node_modules/typescript/lib/lib.d.ts. It contains type declarations for all the things that may exist in javascript runtime environment.

Try running

tsc src/app/pages/details/testts.ts --diagnostics --noLib

or just remove that file temporarily, then you will get report about just one file, as expected:

error TS2318: Cannot find global type 'Array'.
error TS2318: Cannot find global type 'Boolean'.
error TS2318: Cannot find global type 'Function'.
error TS2318: Cannot find global type 'IArguments'.
error TS2318: Cannot find global type 'Number'.
error TS2318: Cannot find global type 'Object'.
error TS2318: Cannot find global type 'RegExp'.
error TS2318: Cannot find global type 'String'.
Files:               1
Lines:               1
Nodes:               7
Identifiers:         1
Symbols:             5
Types:              20
Memory used:    12438K
I/O read:        0.00s
I/O write:       0.00s
Parse time:      0.01s
Bind time:       0.00s
Check time:      0.00s
Emit time:       0.02s
Total time:      0.04s

It seems rather strange that typescript can not typecheck trivial example without having access to declarations for Array and others in separate file, but that's the way it is.

Also, there is --lib compiler option which selects what library files are included. For example, if you want to restrict available global symbols to es5 only, you can use

tsc src/app/pages/details/testts.ts --lib es5

which will use node_modules\typescript\lib\lib.es5.d.ts