WebStorm 2024.2 Help

TypeScript

WebStorm supports developing, running, and debugging TypeScript source code. WebStorm recognizes .ts and .tsx files and provides full range of coding assistance for editing them without any additional steps from your side. TypeScript files are marked with the TypeScript file icon.

TypeScript-aware coding assistance includes completion for keywords, labels, variables, parameters, and functions, error and syntax highlighting, formatting, numerous code inspections and quick-fixes, as well as common and TypeScript-specific refactoring. WebStorm also verifies TypeScript code on the fly and shows errors in a dedicated Problems tool window.

Compilation errors are reported in the TypeScript tool window. Learn more from Compiling TypeScript into JavaScript.

Create a new application

WebStorm can create a basic TypeScript project, so you can set up a project with minimal configuration. The generated project includes a package.json, a tsconfig.json, and an index .ts file with a console.log() welcome message. You can additionally generate an example web application.

By default, WebStorm automatically creates a Git repository and puts the generated sources under Git control.

  1. Click New Project on the Welcome screen or select File | New | Project from the main menu. The New Project dialog opens.

  2. In the left-hand pane, choose New Project.

  3. In the right-hand pane, select TypeScript and specify the path to the folder where the project-related files will be stored.

  4. To generate an example web application, select the Generate a playground project with onboarding tips checkbox. WebStorm will create a project based on the Vite template and provide hints ensuring smooth onboarding.

    Create a basic TypeScript project
  5. Click Create.

Start with an existing TypeScript application

If you are going to continue developing an existing TypeScript application, just open it in WebStorm. Optionally download the required npm dependencies.

Open the application sources that are already on your machine

  • Click Open on the Welcome screen or select File | Open from the main menu. In the dialog that opens, select the folder where your sources are stored.

Check out the application sources from your version control

  1. Click Clone Repository on the Welcome screen.

    Alternatively, select File | New | Project from Version Control or Git | Clone or VCS | Get from Version Control from the main menu.

    Instead of Git in the main menu, you may see any other Version Control System that is associated with your project. For example, Mercurial or Perforce.

  2. In the dialog that opens, select your version control system from the list and specify the repository to check out the application sources from. For more information, refer to Check out a project (clone).

Project security

When you open a project that was created outside WebStorm and was imported into it, WebStorm displays a dialog where you can decide how to handle this project with unfamiliar source code.

Untrusted project warning

Select one of the following options:

  • Preview in Safe Mode: in this case, WebStorm opens the project in a preview mode. It means that you can browse the project's sources but you cannot run tasks and script or run/debug your project.

    WebStorm displays a notification on top of the editor area, and you can click the Trust project link and load your project at any time.

  • Trust Project: in this case, WebStorm opens and loads a project. That means the project is initialized, project's plugins are resolved, dependencies are added, and all WebStorm features are available.

  • Don't Open: in this case, WebStorm doesn't open the project.

Learn more from Project security.

Verify TypeScript

WebStorm verifies TypeScript code mainly based on the data from the TypeScript Language Service which also compiles TypeScript into JavaScript.

Descriptions of the errors detected in the current file and quick-fixes for them are available from the editor and from the File tab of the Problems tool window.

Errors across the entire project and quick-fixes for them are shown in the Project Errors tab of the Problems tool window. To open the tool window, click the Inspection widget in the upper-right corner of the editor:

Inspection widget

For more information, refer to View problems and apply quick-fixes in the editor and Problems tool window.

Verify TypeScript in the current file

  • In the editor, hover over the highlighted problem. WebStorm shows a tooltip with a description of the problem.

    View description of an error in the editor

    Apply the suggested quick-fix or click More actions and select the relevant one from the list.

    Apply quick-fix in the editor
  • Alternatively open the File tab of the Problems tool window Alt+6, where you can view problem descriptions, apply quick-fixes, navigate to the fragments in the source code where errors occurred, as well as fix them in the Editor Preview pane without leaving the tool window. Learn more from Problems tool window.

Verify TypeScript in the entire project

  1. To open the Problems tool window, click the Inspections widget in the upper-right corner of the editor.

    Inspection widget

    Alternatively select View | Tool windows | Problems from the main menu or press Alt+6.

  2. Open the Project Errors tab, which shows the errors across the entire project, with error messages grouped by files in which they were detected.

    Problems tool window, TypeScript. Project Errors tab shows syntax errors across the project

    Here you can view problem descriptions, apply quick-fixes, navigate to the fragments in the source code where errors occurred, as well as fix them in the Editor Preview pane without leaving the tool window. Learn more from Problems tool window.

Configure integration with the TypeScript Language Service

In most cases, everything works out of the box and no manual configuration is required. However, if you want to use a custom typescript package or pass some command-line options to the TypeScript Language Service, you can customize the default settings.

  1. In the Settings dialog (Ctrl+Alt+S) , go to Languages & Frameworks | TypeScript.

    Alternatively, click the Language Services widget on the Status bar and then click Open settings.

    TypeScript widget

    The TypeScript page opens.

  2. Specify the Node.js interpreter to use.

    If you choose the Project alias, WebStorm will automatically use the project default interpreter from the Node interpreter field on the Node.js page . In most cases, WebStorm detects the project default interpreter and fills in the field itself.

    You can also choose another configured local or remote interpreter or click the Browse button and configure a new one.

    For more information, refer to Configuring remote Node.js interpreters, Configuring a local Node.js interpreter, and Using Node.js on Windows Subsystem for Linux.

  3. In the TypeScript field, specify the version of the TypeScript to use (WebStorm displays the currently chosen version).

    • By default, the typescript package from the project's node_modules folder is used.

    • Bundled: choose this option to use the typescript package that is shipped with WebStorm without attempting to find another one.

    • Select: choose this option to use a custom typescript package instead of the one bundled with WebStorm. In the dialog that opens, choose the path to the relevant package.

    • If your project package manager is Yarn 2, you have to use the typescript package installed via Yarn 2. In this case, yarn:package.json:typescript is by default selected.

      Learn more about package managers from npm and Yarn.

  4. Make sure the TypeScript Language Service checkbox is selected. As a result:

    • Syntax and error highlighting is based on the annotations from the TypeScript Language Service.

    • Completion lists contain both suggestions from the TypeScript Language Service and suggestions calculated by WebStorm.

    • TypeScript code is compiled into JavaScript.

  5. Use the controls below to configure the behaviour of the TypeScript Language Service.

    • Show project errors - By default, this checkbox is selected and the TypeScript Language Service checks the code across the entire project. The detected errors are listed in the Project Errors tab of the Problems tool window grouped by files where they occurred.

      To avoid performance issues, clear the Show project errors checkbox so the TypeScript Language Service checks files only when you open them. The Project Errors tab will then list the errors that are detected only in the files that you have already opened.

    • Show suggestions - By default, this checkbox is selected and WebStorm shows potential problems that the TypeScript Language Service detects in your code in addition to actual errors.

      Show suggestions is enabled
      Show suggestions is disabled
    • Use types from server - Select this checkbox to evaluate types of symbols based on the data from the TypeScript compiler.

      By default, the checkbox is cleared so type evaluation, resolution, code inspections and refactorings are based on the type information from the WebStorm internal TypeScript engine.

      This default behavior may result in performance issues and in bugs in type resolution because the type evaluation algorithm that the TypeScript Language Service uses differs from the algorithm of the TypeScript Compiler. Selecting the checkbox may help avoid these problems.

  6. In the Options field, specify the command-line options to be passed to the TypeScript Language Service when the tsconfig.json file is not found. See the list of acceptable options at TSC arguments. Note that the -w or --watch (Watch input files) option is irrelevant.

Restart the TypeScript Language Service

  • Click the Language Services widget on the Status bar and then click The Restart TypeScript Service icon.

    TypeScript widget on the Status bar: restart the TypeScript Language service

Localize error messages

Localized error messages
  1. In the Settings dialog (Ctrl+Alt+S) , go to Languages & Frameworks | TypeScript.

  2. In the TypeScript field, specify a typescript package different from Bundled. This may be the typescript package from your project node_modules folder or from any other location.

  3. In the Options field, type --locale <abbreviation of the language to use>. Currently, Korean (ko) and Japanese (ja) are supported.

Edit TypeScript code

WebStorm brings you smart coding assistance for TypeScript including context-aware code completion, auto-import for symbols, documentation look-up, parameter hints, navigation, TypeScript-aware syntax highlighting and linting, refactoring and more.

Auto import

WebStorm can generate import statements for modules, classes, components, and any other TypeScript symbols that are exported. By default, WebStorm adds import statements when you complete TypeScript symbols.

See Auto import to learn how to optimize import statements and configure their style.

When you type your code or paste a fragment with a symbol that is not yet imported, WebStorm can also generate an import statement for this symbol. If there is only one source to import the symbol from, WebStorm inserts an import statement silently. Otherwise, use an auto-import tooltip or a dedicated import quick-fix.

Add import statements on code completion

Auto import on completion
  1. In the Settings dialog (Ctrl+Alt+S) , go to Editor | General | Auto Import. The Auto Import page opens.

  2. In the TypeScript/JavaScript area, make sure the Add TypeScript imports automatically and On code completion checkboxes are selected.

Add import statements on typing or pasting code

Auto import on type or on paste
  1. In the Settings dialog (Ctrl+Alt+S) , go to Editor | General | Auto Import. The Auto Import page opens.

  2. In the TypeScript/JavaScript area, make sure the Add TypeScript imports automatically and Unambiguous imports on the fly checkboxes are selected.

Use auto-import tooltips

  • If an import statement for a TypeScript symbol was not added on completion or editing, WebStorm shows you a popup that suggests importing the symbol. To accept the suggestion, press Alt+Enter:

    If there's more than one possible source of import, WebStorm informs you about that:

    Auto import tooltip: multiple choices

    Pressing Alt+Enter in this case opens a list of suggestions:

    Auto import tooltip: multiple choices, suggestion list
  • To hide auto-import tooltips, open the Settings dialog (Ctrl+Alt+S) , go to Editor | General | Auto Import, and clear the With auto-import tooltip checkbox.

Use import quick-fixes

If an auto-import tooltip doesn't show up, you can always press Alt+Enter and add an import statement via a quick-fix.

  • To generate an import, select Insert import from:

    Auto import with quick-fix: no choices

    If there is only one source to import a symbol from, WebStorm generates an import statement:

    Auto import with quick-fix: import statement added
  • If there are several sources to import a symbol from, select the relevant one from the suggestion list:

    Auto import with quick-fix, multiple choices: suggestion list
  • If the TypeScript Language Service is enabled in your project, you can also use its suggestion:

    Auto import with suggestion from the TypeScript Language Service: multiple choices

Configure the use of the type modifier in import statements

  1. Press Ctrl+Alt+S to open settings and then select Editor | Code Style | TypeScript.

  2. Switch to the Imports tab and in the Use type modifier in imports field configure the use of the type modifier in import statements for types. Note that the settings apply only for types, no type modifier is inserted for not types, no matter which option you select.

    With this option, a type modifier is added if "importsNotUsedAsValues": "error" or "verbatimModuleSyntax": true is specified in your tsconfig.json.

    Otherwise, if no import type is preferred, no type modifier is inserted.

    Auto

    With this option, a type modifier is always inserted, no matter whether "importsNotUsedAsValues": "error" or "verbatimModuleSyntax": true is specified in your tsconfig .json:

    Always with type

    With this option, WebStorm never uses the type modifier, regardless of the "importsNotUsedAsValues": "error" or "verbatimModuleSyntax": true flags in your tsconfig.json:

    Never

If there is already an import statement with a type modifier, this modifier is always used for further importing types regardless of the selected option and of the settings in your tsconfig.json.

For constants, a {<constant name>} specifier is used instead of a type modifier. For TypeScript 4.5 and later with Auto or Always selected, all the previous modifier are converted to {type} specifiers. Otherwise, all the existing specifiers remain unchanged.

Learn more from the TypeScript official website.

Configure the appearance of import statements

  • In the Settings dialog (Ctrl+Alt+S) , go to Editor | Code Style | TypeScript, and use the controls in the Imports tab. For more information, refer to Imports tab.

Documentation look-up

WebStorm lets you get reference for symbols from your project and from its dependencies, for symbols defined in external libraries, and for standard JavaScript APIs because TypeScript implements all of them.

By default, documentation is shown in the Documentation popup but you can always open it in the Documentation tool window or even configure it to appear in the Documentation tool window by default.

Quick documentation lookup: switch between the popup and the tool window

View Quick Documentation

  • Hover over the necessary symbol in the editor.

    Alternatively, place the caret at the symbol and press Ctrl+Q or select View | Quick Documentation from the main menu.

  • To open Quick Documentation in the Documentation tool window by default, click the Options icon in the popup or on the toolbar of the tool window and clear the Show Documentation Popup First option.

  • Click the Show Options Menu icon in the popup to change the font size, display the quick documentation toolbar, or go to the source code.

    For more information, refer to Configure the Quick Documentation popup.

You can open a piece of documentation for a specific code element in the tool window and keep viewing documentation for other elements in your current file as described in Keep documentation in the tool window.

In the tool window, code documentation is displayed on the unpinned tab (the tab marked with the asterisk symbol (*). You can view this documentation by hovering over a symbol or placing the caret at it. For more information, refer to Quick Documentation tool window: modes.

View the MDN documentation for a symbol at caret

For standard JavaScript methods available in TypeScript, WebStorm also shows a link to the corresponding MDN article.

Quick Documentation Lookup: MDN
  1. In the Documentation window Ctrl+Q, click the MDN link.

  2. Alternatively, press Shift+F1 or choose View | External Documentation from the main menu.

WebStorm opens the MDN article in the default WebStorm browser.

View inlay hints

Inlay hints appear in the editor and provide you with additional information about your code to make it easier to read and navigate.

Inlay hints are available in .ts and .tsx files.

  • If the TypeScript Language service is enabled and TypeScript version is 4.4 or higher, inlay hints are retrieved from the TypeScript Language Service.

  • For earlier TypeScript versions or if the TypeScript Language Service is disabled, inlay hints are retrieved from the internal WebStorm evaluator.

Unlike many inspections and highlights, which always analyze the whole file, hints are retrieved only for the currently visible area and are updated on scrolling. This behavior aims at higher performance.

Parameter hints

Parameter hints show the names of parameters in methods and functions to make your code easier to read.

ws_typescript_parameter_hints.png

With TypeScript version 5.2 and higher, parameter hints turn into links on hover. Clicking such link with Ctrl pressed brings you to the method definition.

Configure parameter hints

  1. Open the Settings dialog (Ctrl+Alt+S) and go to Editor | Inlay Hints.

  2. Expand TypeScript under Parameter names.

  3. Specify the context in which you want parameter hints shown by selecting the corresponding checkboxes.

    The preview shows how the changes you make in the settings affect the code appearance.

  4. To hide parameter hints for any value type in any context, clear the TypeScript checkbox under Parameter names.

Return type hints

WebStorm can display function return types in function calls and call chains.

  • Function return type hints are retrieved from the TypeScript Language Service.

  • Return type hints in method chains are displayed if the method calls are split between multiple lines and return at least 2 different types.

    Return type hints in method chains are provided by the internal WebStorm evaluator.

Configure function return type hints

  1. Open the Settings dialog (Ctrl+Alt+S) and go to Editor | Inlay Hints.

  2. Under Types, expand the TypeScript node, and then select the Function return types checkbox.

    Function return type hints
  3. To show return type hints in method chains, expand the Method chains node and select the TypeScript checkbox.

    Return types for chained methods

The preview shows how the changes you make in the settings affect the code appearance.

Type hints

Type hints show the types of variables, fields, or parameters. The types of variables and fields are displayed next to their definition. Type hints for parameters are shown in function calls. Type hints are inferred from JSDoc comments or static analysis of your code.

With TypeScript version 5.3 and higher, type hints turn into links on hover. Clicking such link with Ctrl pressed brings you to the type definition.

Configure type hints

  1. Open the Settings dialog (Ctrl+Alt+S) and go to Editor | Inlay Hints.

  2. Expand Types | TypeScript.

  3. Expand the Type annotations node and specify the context where you want type hints to be shown.

    The preview shows how the changes you make in the settings affect the code appearance.

    TypeScript type hints
  4. To hide parameter type and return type hints for any value type in any context, clear the TypeScript checkbox under Types.

Numeric enum values

WebStorm shows hints for numeric enums to make your code easier to read. These hints are retrieved from the TypeScript Language Service and are displayed by default.

Hide numeric enum values

  1. Open the Settings dialog (Ctrl+Alt+S) and go to Editor | Inlay Hints.

  2. Expand the Values node and select the TypeScript checkbox. WebStorm will hide the hints and show only the values of explicitly initialized constants. The preview shows how the changes you make in the settings affect the code appearance.

JavaScript libraries in TypeScript

When working with JavaScript libraries in TypeScript, you need to install type declarations for them. WebStorm reminds you to install them via npm or yarn and updates your package.json file accordingly.

Install the type declarations

  1. Place the caret at the warning and press Alt+Enter.

    Missing type declaration
  2. Select the suggestion and press Enter.

    Add type declarations

Syntax highlighting

You can configure TypeScript-aware syntax highlighting according to your preferences and habits.

  1. In the Settings dialog (Ctrl+Alt+S) , go to Editor | Color Scheme | TypeScript.

  2. Select the color scheme, accept the highlighting settings inherited from the defaults or customize them as described in Colors and fonts.

Code navigation

You can quickly navigate through your TypeScript project in the WebStorm editor using different actions and popups.

Go to declaration of a symbol

You can navigate from a variable, a field, a method, or any other symbol to its declaration or view the symbol definition in a popup without jumping anywhere from the code you are editing.

  • To jump to the declaration of a symbol, place the caret at a usage of the symbol and press Ctrl+B or select Navigate | Declaration or Usages from the main menu.

    Go to declaration

    Alternatively, use Ctrl+Click: keeping Ctrl pressed, hover over the symbol. When the symbol turns into a hyperlink, click the hyperlink without releasing Ctrl.

    Go to Declaration with Ctrl+click
  • To view the definition of a symbol in a popup, place the caret at its usage and press Ctrl+Shift+I or select View | Quick Definition from the main menu.

    View the definition of a symbol on a popup

Go to usages of a symbol

You can view a list of usages of a symbol and select the one to jump to.

  1. To get a list of usages of a symbol, place the caret at the declaration of the symbol and do one of the following:

    • Press Ctrl+B or select Navigate | Declaration or Usages from the main menu.

    • Press Ctrl+Alt+F7 or select Edit | Find Usages | Shows Usages from the main menu.

  2. In the popup, use the icons on the toolbar to configure the appearance of the list and filter out irrelevant usages, for example, in import statements.

    Configure the Go To Usages list
  3. From the list, select the usage of the symbol where you want to jump and press Enter.

    Go to usages of a symbol

Go to type declaration of a symbol

You can navigate from a variable, a field, a method, or any other symbol to its type declaration. Alternatively, open the type definition in a popup without jumping to the type declaration.

WebStorm also shows the inferred type of an object. You can view the inferred type information in a tooltip or in the documentation popup.

  • To jump from a symbol to the declaration of its type, place the caret at a usage of the symbol and press Ctrl+Shift+B or select Navigate | Type Declaration from the main menu.

    Go to type declaration
  • To view the type definition of a symbol in a popup, place the caret at the symbol for which you want to view the type definition and select View | Quick Type Definition.

    Quick type definition

    For an instance of a class, this will reveal the class itself instead of where this instance is defined.

Note the difference between Go To Declaration and Go To Type Declaration. Suppose you have a file app.ts with the following code:

import {Dog} from "./dog"; let dog = new Dog(); dog.bark();

If you place the caret at dog in dog.bark(), then Go To Declaration brings you to the declaration of the variable in let dog = new Dog (), while Go To Type Declaration brings you to the declaration of the class Dog.

Go To Declaration and Go To Type Declaration lead to different places

View inferred type information of a symbol

  • Hold Ctrl and hover over the symbol.

    Inferred type of a symbol is shown in a tooltip
  • Alternatively, hover over a symbol. WebStorm immediately displays the reference for it in the Documentation popup.

    Inferred type of an object is shown in the documentation popup

    Learn more from Documentation look-up above.

Navigate between subclasses, superclasses, overrides, and implementations

You can keep track of class implementations and overriding methods either using the gutter icons in the editor or pressing the appropriate shortcuts.

Go to a subclass

  • Press Ctrl+Alt+B or click the Overridden method icon in the gutter and then select the relevant class from the list.

    Go to descendent class
  • Alternatively, select Navigate | Implementation(s) from the main menu or Go To | Implementation(s) from the context menu and then select the relevant class from the list.

Go to a superclass or overridden method

  • Place the caret at a subclass and press Ctrl+U. WebStorm brings you to the declaration of the superclass and places the caret at its name.

  • Click the Overriding method icon in the gutter next to an overriding method. WebStorm brings you to the superclass with the caret at the overridden method.

    Go to overridden method

    Alternatively, place the caret at the overriding method and press Ctrl+U or select Navigate | Super Method from the main menu or Go To | Super Method from the context menu.

Go to an interface or implemented method

  • Place the caret at an implementation of an interface and press Ctrl+U.

    Go to interface from class

    WebStorm brings you to the declaration of the interface and places the caret at its name.

  • Click the Implementing method icon in the gutter next to the implementing method. WebStorm brings you to the corresponding interface with the caret at the implemented method.

    Go to implemented method

    Alternatively, place the caret at the implementing method and press Ctrl+U or select Navigate | Super Method from the main menu or Go To | Super Method from the context menu.

Refactoring in TypeScript

WebStorm provides both common refactoring procedures, such as rename/move, and so on, and TypeScript-specific refactoring procedures, such as change signature, introduce parameter, introduce variable. For more information, refer to Refactoring TypeScript and Refactoring JavaScript.

Run and debug your application

With WebStorm, you can run and debug client-side TypeScript code and TypeScript code running in Node.js. Learn more from Running and debugging TypeScript.

Last modified: 11 October 2024