Transpiling TypeScript to JavaScript
TypeScript code is not processed by browsers that work with JavaScript code. Therefore to be executed, TypeScript code has to be translated into JavaScript. This operation is referred to as transpilation and the tools that perform it are called transpilers.
In addition to this, transpilation also can involve generation of source maps that set correspondence between lines in your TypeScript code and in the generated JavaScript code, otherwise your breakpoints will not be recognised and processed correctly.
With WebStorm, you can have your TypeScript code transpiled into JavaScript in two ways: using the built-in TypeScript compiler or through integration with the typescript tool configured as a file watcher.
On this page:
- Configuring and Activating the Built-in TypeScript Compiler
- Transpiling TypeScript using the Built-in Compiler
- Resolving References in TypeScript Code without Running the Built-In Compiler
- Transpiling TypeScript using a File Watcher
Configuring and Activating the Built-in TypeScript Compiler
- Open the Settings / Preferences Dialog by choosing for Windows and Linux or for OS X, and click Typescript under Languages & Frameworks.
- On the TypeScript Compiler page that opens, select the Enable TypeScript Compiler check box to activate the compiler. When the check box is selected, the fields below on the page become active and you can configure the behaviour of the compiler.
-
In the other fields of the TypeScript Compiler page, specify the following:
- In the Node interpreter field, specify the location of the Node.js executable file. In most cases, WebStorm detects the Node.js executable and fills in the field automatically.
-
In the Compiler version area, WebStorm displays the default bundled version of the built-in compiler.
To use a custom version of the compiler:
-
Download the
typescriptServices.js
,lib.d.ts
, andlib.es6.d.ts
files from https://github.com/Microsoft/TypeScript/. - Click Edit in the Compiler version area.
- In the Configure TypeScript Compiler dialog box that opens, choose Custom directory and specify the folder where the downloaded files are stored.
-
Download the
-
From the Scope drop-down list, choose the scope to apply the compiler in.
The available options are:
- Project Files: all the files within the project content roots (see Content Root and Configuring Content Roots).
- Project Production Files: all the files within the project content roots excluding test sources.
- Project Test Files: all the files within the project test source roots.
- Open Files: all the files that are currently opened in the editor.
VCS Scopes: these scopes are only available if your project is under version control.
- Changed Files: all changed files, that is, all files associated with all existing changelists.
-
Default: all the files associated with the changelist
Default
.
-
In the Command line options field,
specify the command line options to be passed to the compiler.
See the list of acceptable options at TSC arguments.
Note that, the
-w
or--watch
option (Watch input files) is irrelevant. -
Select the Compile main file only check box
to have WebStorm compile only a specific file and the files that are referenced from it and ignore all the other files in the project.
This may be helpful if you have a dedicated
main.ts
file which only references other files. -
Select the Use output path check box
to have the built-in compiler store the generated JavaScript files and source map in a custom folder.
Specify the path to this folder explicitly or use one of the listed available macros in the format:
$<macros_name>$
. The available macros are:-
FileDir
: the path to the folder where the file is stored. -
FileRelativeDir
: the path to the file directory relative to the project root. -
FileDirRelativeToProjectRoot
: the path to the file directory relative to the project root. -
ModuleFileDir
: the path to the project root folder. -
SourcePath
: the path to the project root folder. -
FileDirRelativeToSourcePath
: the path to the file relative to the project root.
-
- Select the Generate source maps check box to generate source maps that set correspondence between lines in your TypeScript code and in the generated JavaScript code, otherwise your breakpoints will not be recognised and processed correctly.
-
To have the compiler "wake up" upon any change to a TypeScript file, select the Track changes check box.
When this check box is cleared, the built-in compiler ignores changes to TypeScript files. To re-activate the compiler, open the TypeScript Compiler Tool Window ( ), and click the Compile All button
on the toolbar.
If you have not opened the TypeScript Compiler Tool Window yet and it is not available from the menu, choose , then find and launch the TypeScript Compile All action from the list.
Transpiling TypeScript using the Built-in Compiler
-
To activate a previously configured built-in transpiler, select the Enable TypeScript Compiler check box
on the TypeScript Compiler page.
Alternatively, use the buttons on the pane which WebStorm displays when you open a TypeScript file.
- To enable an already configured compiler, click Enable.
- To configure a compiler, click Configure and specify the settings on the TypeScript Compiler page that opens.
- To leave the compiler disabled and suppress showing the pane in the future, click Dismiss, whereupon the compiler can be later activated only on the TypeScript Compiler page.
-
View the errors detected during compilation, compile separate files and the entire project in the TypeScript Compiler Tool Window.
If you have configured the compiler to track changes, which is the default behaviour, the tool window opens automatically
as soon as you start editing a TypeScript file.
If tracking changes is disabled and you have closed the tool window, to re-open it choose .
-
Click the Compile Current File toolbar button
to run the compiler against the entire file opened in the active editor tab. This approach is helpful if you do not want the compiler to wake up on any change as you edit your code and for this purpose the Track Changes check box on the TypeScript page of the Settings dialog box is cleared.
If you click this button from the Project Errors pane, after compilation WebStorm switches to the Current Errors pane.
-
Click the Compile All toolbar button
to run the compiler against all the TypeScript files in the current project. If you click this button from the Current Errors pane, after compilation WebStorm switches to the Project Errors pane.
-
To clear the contents of the current pane, click the Clear All toolbar button
.
- To navigate to the fragment of code where an error occurred during compilation, select the error message in question and choose Jump to Source on the context menu of the selection.
-
Click the Compile Current File toolbar button
The compiler stores the generated output in a separate file. The JavaScript file has the name of the source TypeScript file and the extension js
,
the source map file has the name of the source TypeScript file and the js.map
extension.
In the Project Tree, the files are shown under the source TypeScript file
which is now displayed as a node.
Resolving References in TypeScript Code without Running the Built-In Compiler
If you do not need the JavaScript output yet, you can
have your TypeScript code analyzed without actually running the built-in compiler
but through checking the scopes set in the tsconfig.json
file
:
- Open the Settings / Preferences Dialog by choosing for Windows and Linux or for OS X, and click TypeScript under Languages&Frameworks.
- On the TypeScript page that opens, select the Resolve objects using tsconfig.json check box.
Transpiling TypeScript using a File Watcher
Using file watchers is recommended if your project consists of several independent parts and you want to transpile them separately. In this case you can configure several file watchers each with its own scope and apply these file watchers independently.
WebStorm supports integration with the typescript tool. The TypeScript transpiler translates TypeScript code into JavaScript and creates source maps that set correspondence between lines in your TypeScript code and in the generated JavaScript code, otherwise your breakpoints will not be recognised and processed correctly.
In WebStorm, transpiler configurations are called File Watchers. For each supported transpiler, WebStorm provides a predefined File Watcher template. Predefined File Watcher templates are available at the WebStorm level. To run a transpiler against your project files, you need to create a project-specific File Watcher based on the relevant template, at least, specify the path to the transpiler to use on your machine.
The easiest way to install the TypeScript transpiler is to use the Node Package Manager (npm), which is a part of Node.js. See Installing and Removing External Software Using Node Package Manager for details.
Depending on the desired location of the TypeScript transpiler executable file, choose one of the following methods:
- Install the transpiler globally at the WebStorm level so it can be used in any WebStorm project.
- Install the transpiler in a specific project and thus restrict its use to this project.
- Install the transpiler in a project as a development dependency.
In either installation mode, make sure that the parent folder of the TypeScript transpiler is added to the PATH
variable.
This enables you to launch the transpiler from any folder.
WebStorm provides user interface both for global and project installation as well as supports installation through the command line.
Before You Start
-
Download and install Node.js. The runtime environment is required for two reasons:
- The TypeScript transpiler is started through Node.js.
- NPM, which is a part of the runtime environment, is also the easiest way to download the TypeScript transpiler.
If you are going to use the command line mode, make sure the path to the parent folder of the Node.js executable file and the path to the
npm
folder are added to thePATH
variable. This enables you to launch the TypeScript transpiler and npm from any folder. - Make sure the Node.js plugin is enabled. The plugin is bundled with WebStorm and activated by default. If the plugin is not activated, enable it on the Plugins page of the Settings / Preferences Dialog as described in Enabling and Disabling Plugins.
- Make sure the File Watchers plugin is enabled. The plugin is bundled with WebStorm and activated by default. If it is not, enable the plugin. See Enabling and Disabling Plugins for details.
Installing the TypeScript Transpiler Globally
Global installation makes a transpiler available at the WebStorm level so it can be used in any WebStorm project.
Moreover, during installation the parent folder of the transpiler is automatically added to the PATH
variable,
which enables you to launch the transpiler from any folder.
To install the transpiler globally, do one of the following:
-
Run the installation from the command line in the global mode:
-
Launch the embedded Terminal
by hovering your mouse pointer over
in the lower left corner of WebStorm and choosing Terminal from the menu (see Working with Embedded Local Terminal for details).
-
Switch to the directory where NPM is stored or define a
PATH
variable for it so it is available from any folder, see Installing NodeJs. -
Type the following command at the command line prompt:
npm install -g typescript
The
-g
key makes the transpiler run in the global mode. Because the installation is performed through NPM, the TypeScript transpiler is installed in thenpm
folder. Make sure this parent folder is added to thePATH
variable. This enables you to launch the transpiler from any folder.For more details on the NPM operation modes, see npm documentation. For more information about installing the TypeScript transpiler, see https://npmjs.org/package/typescript.
-
Launch the embedded Terminal
by hovering your mouse pointer over
-
Run NPM from WebStorm using the Node.js and NPM page of the Settings dialog box.
- Open the Settings / Preferences Dialog by choosing for Windows and Linux or for OS X, and click Node.js and NPM under Languages&Frameworks.
-
On the packagePage page that opens, the Packages area shows all the engineDependent packages
that are currently installed on your computer, both at the global and at the project level.
Click
.
- In the Available Packages dialog box that opens, select the required package to install.
-
Select the Options check box and type
-g
in the text box next to it. - Optionally specify the product version and click Install Package to start installation.
Installing the TypeScript Transpiler in a Project
Installing a transpiler in a specific project restricts its use to this project. To run project installation, do one of the following:
-
Run the installation from the command line:
-
Launch the embedded Terminal
by hovering your mouse pointer over
in the lower left corner of WebStorm and choosing Terminal from the menu (see Working with Embedded Local Terminal for details).
-
Switch to the project root folder and type the following command at the command line prompt:
npm install typescript
-
Launch the embedded Terminal
by hovering your mouse pointer over
-
Run NPM from WebStorm using the Node.js and NPM page of the Settings dialog box.
- Open the Settings / Preferences Dialog by choosing for Windows and Linux or for OS X, and click Node.js and NPM under Languages & Frameworks.
-
On the Node.js and NPM page that opens, the Packages area shows all the Node.js-dependent packages
that are currently installed on your computer, both at the global and at the project level.
Click
.
- In the Available Packages dialog box that opens, select the required package.
- Optionally specify the product version and click Install Package to start installation.
Project level installation is helpful and reliable in template-based projects
of the type Node Boilerplate or Node.js Express, which already have the node_modules
folder.
The latter is important because NPM installs the TypeScript transpiler
in a node_modules
folder. If your project already contains such folder, the TypeScript transpiler is installed there.
Projects of other types or empty projects may not have a node_modules
folder.
In this case npm goes upwards in the folder tree and installs the TypeScript transpiler
in the first detected node_modules
folder. Keep in mind that this detected node_modules
folder may be outside your current project root.
Finally, if no node_modules
folder is detected in the folder tree either,
the folder is created right under the current project root and the TypeScript transpiler is installed there.
In either case, make sure that the parent folder of the TypeScript transpiler is added to the PATH
variable.
This enables you to launch the transpiler from any folder.
Creating a File Watcher
WebStorm provides a common procedure and user interface for creating File Watchers of all types. The only difference is in the predefined templates you choose in each case.
- To start creating a File Watcher, open the Settings dialog box by choosing for Windows and Linux or for OS X on the main menu, and then click File Watchers under the Tools node. The File Watchers page that opens, shows the list of File Watchers that are already configured in the project.
-
Click the Add button
or press Alt+Insert and choose the TypeScript predefined template from the pop-up list.
-
In the Program text box, specify the path to the executable file
tsc.cmd
ortsc
in thenode_modules\.bin
folder.-
If you installed TypeScript in the current project, the file is stored in the
node_modules\.bin
folder under the project root. -
In case of global installation, the executable file is stored in the
node_modules\.bin
folder in the user home.
Type the path manually or click the Browse button
and choose the file location in the dialog box that opens.
-
If you installed TypeScript in the current project, the file is stored in the
- Proceed as described on page Using File Watchers.
Transpiling the Code
When you open a TypeScript file, WebStorm checks whether an applicable file watcher is available in the current project. If such file watcher is configured but disabled, WebStorm displays a pop-up window that informs you about the configured file watcher and suggests to enable it.
If an applicable file watcher is configured and enabled in the current project, WebStorm starts it automatically upon the event specified in the New Watcher dialog.
- If the Immediate file synchronization check box is selected, the File Watcher is invoked as soon as any changes are made to the source code.
- If the Immediate file synchronization check box is cleared, the File Watcher is started upon save ( , Ctrl+S) or when you move focus from WebStorm (upon frame deactivation).
The transpiler stores the generated output in a separate file. The file has the name of the source TypeScript file and the extension js
or js.map
depending on the transpiler type. The location of the generated files is defined in the Output paths to refresh text box
of the New Watcher dialog. Based on this setting, WebStorm detects the transpiler output.
However, in the Project Tree, they are shown under the source .ts
file
which is now displayed as a node.