The definition files generated by the TypeScript compiler are indeed mostly a repeat of the code you wrote but with some notable differences:
- They don’t contain implementation, only declarations
- They only contain publicly accessible types
The use case for these declaration files is for distributing libraries. You can distribute a library without the original TypeScript code (as that does not matter at runtime anyway) and distribute just the compiled JavaScript. JavaScript consumers will use the JavaScript and not care about its origin. TypeScript consumers will also use the JavaScript at runtime, but the .d.ts
files will allow the compiler to check the code even though the original TypeScript source for the library is not present.
This same approach could be also used to break up a large project into several smaller projects that use the declaration files to establish the interface between them. Each project can be recompiled independently without recompiling all of them, reducing compilation times. The TypeScript compiler is actually planning to add buit-in support for this in the future.