TypeScript - compile all ts files to one js

12 points
Created by:

In this article we would like to show how to compile all *.ts files into one js file. There are two module types that let us to use single file compilation: amd and system. In below description we used amd module,

Quick solution:

1. set in the tsconfig.json file following compiler options:

    "compilerOptions" : {
        "module" : "amd",
        "outFile" : "./out.js",

2. compile TypeScript files funning following command:


1. Example project

This section contains example files with compilation and running description. 

Node: download example source code from here.

Project directory structure:

 +-- Project/
      + tsconfig.json
      + Person.ts
      + Printer.ts
      + Main.ts
      + index.htm

1.1. It is necessary to define proper configuration

tsconfig.json file:

    "version" : "3.0.3",
    "compilerOptions" : {
        "module" : "amd",  /*  <----------------- REQUIRED  */
        "removeComments" : true,
        "outFile" : "./out.js",  /*  <----------- REQUIRED  */
        "baseUrl" : "./",
        "sourceMap" : true,  /*  <--------------- only to debug ts files */
        "alwaysStrict" : true,
        "declaration" : true,
    "include": [
    "exclude": [

Note: "outFile" : "./out.js" defines compilation result (one file).

1.2. Now we can prepare some *.ts files that will be compiled to one file.

Person.ts file:

export class Person
	public constructor(public name : string, public age : number)
		// nothing here ...
	public toString() : string
		return `{ Name: ${this->name}, Age: ${this->age} }`;

Printer.ts file:

import { Person } from "./Person";

export class Printer
	public static printPerson(person : Person) : void
	public static printPersons(persons : Array<Person>) : void
		for(let entry of persons)

Main.ts file:

import { Person } from "./Person";
import { Printer } from "./Printer";

let persons = new Array<Person>();

persons.push(new Person('John', 30));
persons.push(new Person('Adam', 23));
persons.push(new Person('Kate', 41));


1.3. Each web page requires index.html that runs a page - in this case is same.

require.js was attached as first because out.js file depends on it. out.js contains all ts source code copiled into sinle js file. Last important thing is to tell application to run the web page using main module - in our case, main module is Main, so require(['Main']) was called.

index.htm file:

<!doctype html>
  <script src=""></script>
  <!--     or:        -->
  <script src="out.js"></script>

Note: above source code uses modules loader.

1.4. Compilation and running

Simple steps:

  1. open command line (e.g. Windows Command Propmpt or Bash),
  2. go to project directory,
  3. run tsc command to compile all files to one,
  4. open index.htm in web browser

Console preview:

$ cd /C/Project
$ tsc

Directory screenshot:

Result of TypeScript compilation into single file.
Result of TypeScript compilation into single file.


  • after tsc command run we should see 3 new files:
    • out.d.ts contains all declarations
      (useful for intellisense and intengration out.js file with other typescript projects)
    • contains development mapping for debug process
    • out.js contains all sourcecode transpiled to single javascript¬†file

Web Browser preview: 

Result of TypeScript compilation into single file in Web Browser.
Result of TypeScript compilation into single file in Web Browser.


Download all source code from one place here.

2. Troubleshooting

3. References

  1. RequireJS - Home Page
  2. tsconfig.json - TypeScript Docs

Native Advertising
50 000 ad impressions - 449$
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

‚̧ԳŹūüíĽ ūüôā