4

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?

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, it helped me to understand what is going on. But this basically means 2 seconds compile time for one line... And unfortunately, this same time is taken while recompile triggered by watch -w. Which means additional 2s before my tests start running. No way to safely increase performance?
To make it faster, you can use only a subset of the library that your code actually needs. On my computer, one-line file takes 0.24 s with --lib es5, 1.60s with --lib es5,dom, and about the same with default lib. You can create custom library file with only the definitions that you want, and use it with tsc test.ts your-lib-file.d.ts --noLib.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.