Search code examples
typescript

Combine multiple typescript files into one typescript definition file


I need this for distributing libraries in TypeScript in a single file. Is there a way to merge multiple typescript files into (one js file + one typescript definition) file?


Solution

  • To create a library you could compile it as follows:

    tsc --outfile mylib.js --declaration app.ts
    

    This means you get the compiled JavaScript and a TypeScript definition file, and it is still just as simple to use in TypeScript as if it was a single TypeScript file.

    You don't need to specify all the files you want to combine, the compiler will walk all the dependencies and bring them all into one file in the correct order. In the example above I have specified only app.ts, but all of the references will be walked and they will all make it into the combined mylib.js and the associated mylib.d.ts files.