« All posts

03.29.2022 - TypeScript / A note about lib.dom.d.ts

In TypeScript, all the type definitions for the Web API are implemented in the lib.dom.d.ts file.

One interesting thing is, this file is automatically generated from the Web Standards and does not mean to be edited by hand.

The w3c/webref project generates the machine-readable version of the Web Standards. New data is generated every 6 hours.

The microsoft/TypeScript-DOM-lib-generator project pulls the machine-readable webref and uses it as a reference data to generate the TypeScript interface for every Web API. The output is the generated lib.dom.d.ts, which will be used in TypeScript.

In this repository, we can modify the behavior of the generator like:

  • Adding new type definition in the inputfiles/addedTypes.jsonc file.
  • Overriding the default type definition from the specs in the inputfiles/overridingTypes.jsonc file.
  • Modify the comment string of a type (which will also being shown in the documentation popup of the editor) in the inputfiles/comments.jsonc file.

When the npm run build command is triggered, the src/build.ts script will pull the interfaces from webref and check the inputfiles/removedTypes.jsonc to see if there is any types needed to be removed, then it checks the addedTypes.jsonc and overridingTypes.jsonc to modify the remaining interfaces accordingly.

Finally, the lib.dom.d.ts will be generated to be submitted to the TypeScript repository.

Read more