13

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?

6
  • Sorry what do you mean by removes all the static information? Typescript will contact to a single file, are you using references like this?: ///<reference path='dgrid.d.ts' /> Commented May 21, 2013 at 0:37
  • @7zark7 made the question more explicit. Should be clear now. Commented May 21, 2013 at 0:41
  • Should TypeScript libraries be distributed as TypeScript or as JavaScript with an associated definition file? Commented May 21, 2013 at 7:45
  • @MiMo I think steve's answer is where you were going. --declaration for multiple files generates a .d.ts for each file. I tested it this morning: github.com/basarat/ts-test/tree/master/tests/compileToSingle I would be okay if it was (one js + one d.ts) Commented May 21, 2013 at 10:21
  • @BASarat There's a flaw in your test. You specify --out or --declaration but you need to specify both to get a single .js and single .d.ts. Commented May 21, 2013 at 16:28

2 Answers 2

9

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.

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

11 Comments

I am afraid the author wants to combine multiple TypeScript files in a single TypeScript file, and not use a JavaScript file as a .ts file. Example: if he has File1.ts, File2.ts, ..., FileN.ts, he wants to run a command that generates TheUltimateSingleFileTypeScriptLibrary.ts so he can deploy his TypeScript library for other developers to use in their projects. Maybe I'm wrong, but that was my understanding of his question.
I'm going to repeat myself in case someone reads this thread but not the other. If --out and --declaration are specified you do get a single .d.ts file.
@BASarat I have corrected a point in your edit - you don't need to specify all of your files as the compiler will walk the chain of references for you.
@jayarjo yes - all the flags you specify on the command line are also available in the compiler options section of your tsconfig.json file.
Option 'out' is deprecated and will stop functioning in TypeScript 5.5. Use 'outFile' instead.
|
1

Just in case anyone is searching some solution to combine typescript modules into one single d.ts file https://github.com/timocov/dts-bundle-generator dts-bundle-generator worked for me. Other libraries like npm-dts or dts-bundle are outdated.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.