3

There is a similar question. That covers what typescript does with the option. The below question and answer covers where the lib files come from at runtime.

When I looked up the description for the --lib compiler option it says:

List of library files to be included in the compilation.

What are these library files and how are they included? The repository I'm studying has the following settings?

"compilerOptions": {
  "target": "es5",
  "module": "es2015",
  "lib": ["es2015", "dom"]
},

How would the output be different if --lib were not set to ["es2015", "dom"]?

Community
  • 1
  • 1
Ole
  • 41,793
  • 59
  • 191
  • 359
  • Possible duplicate of [What does the tsconfig option "lib" do?](http://stackoverflow.com/questions/39303385/what-does-the-tsconfig-option-lib-do) – Louis May 04 '17 at 15:21
  • That question has a broader scope. @Meirion Hughes was able to provide simple insight with a short answer without linking to a very long article, so I'm going to leave it as is. – Ole May 04 '17 at 15:28

1 Answers1

7

They tell the typescript compiler that those type libraries are available during the run-time and it won't complain that your target es version is missing features.

Your config is targeting es5, but you're telling typescript that "es2015" features will be available - so for example Promise and Map. Similarly you have "dom", so typescript knows that you have window and browser-dom features.

As far as I'm aware, it won't alter the output... typescript will simply error when you try to use features that are not present in your target es version.

You'd do this when you want to target old browsers, but also want to use, for example, Promises. So we tell the compiler its available with lib:["es2015.promise"] then you run something like core-js or bluebird at execution-time to polyfill the Promise functionality.

Meirion Hughes
  • 24,994
  • 12
  • 71
  • 122
  • OK great! I'm currently looking at this for Angular4 and it looks like it includes core-js by default, so I assume that es2015 features will be covered by that in all implementations (And dom is naturally covered by the browser). Although based on this article that I'm reading http://blog.mgechev.com/2017/01/21/distributing-an-angular-library-aot-ngc-types/ we should not touch the dom directly ... so is it a good idea to include dom? That might be a different question. – Ole May 04 '17 at 14:36
  • Technically, manipulating the dom is what Angular is supposed to be doing... so yeah, you may want to try without the "dom" types so you don't get tempted to do stuff yourself. – Meirion Hughes May 04 '17 at 15:03
  • That's what I thought - but the author that said that it's not a good idea included it ... so I figured I'd follow up here to see if there are any corner cases: http://stackoverflow.com/questions/43786728/should-the-typescript-lib-setting-dom-be-included-when-developing-an-angular-4-c – Ole May 04 '17 at 15:14
  • Do you know where to find the exhaustive list of definitions that each library covers? Let's say "DOM" for the example ? – Joseph Merdrignac Sep 23 '20 at 07:47
  • Ok got: part of the response is here https://www.typescriptlang.org/tsconfig#lib but for exhaustive one may look directly at the source code, for example "setTimeout" @ "DOM" https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts#L19869 – Joseph Merdrignac Sep 23 '20 at 07:54