Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Developing TypeScript with VS Code

Developing TypeScript with VS Code

Many IDEs support TypeScript plugins, such as: VS Code, Sublime Text 2, WebStorm / PHPStorm, Eclipse, etc.

This section mainly introduces VS Code. VS Code is a cross-platform source code editor developed by Microsoft that runs on Mac OS X, Windows, and Linux, aimed at writing modern web and cloud applications.

VS Code has built-in native support for TypeScript language syntax but does not include the TypeScript compiler tsc. To transpile TS source to JS (run the command tsc HelloWorld.ts), you need to install the TypeScript compiler separately, either globally or in the project workspace.

VS Code tutorial:https://www.runoob.com/vscode/vscode-tutorial.html

In addition, domestic companies Alibaba and ByteDance also have AI IDEs developed based on VS Code:

Install the TypeScript compiler

The simplest installation method is via the Node.js package manager npm. With npm already installed, run the following command to install globally (-g):



npm install -g typescript

After installation, you can verify it by outputting the version number:



tsc --version

Installing Visual Studio Code on Windows

1. Download Visual Studio Code。

2. Double-click the VSCodeSetup.exe icon installation.

3. After installation, opening Visual Studio Code shows an interface similar to the following:

4. We can click the currently edited code file in the left window and select open in command prompt(Open in Terminal); then we can use the lower-right part of the screen to tsc the command to execute TypeScript file code.

Installing Visual Studio Code on Mac OS X

For installing and configuring Visual Studio Code on Mac OS X, see: https://code.visualstudio.com/Docs/editor/setup

Installing Visual Studio Code on Linux

For installing and configuring Visual Studio Code on Linux, see: https://code.visualstudio.com/Docs/editor/setup


Getting started example: Hello World

We take a minimal Hello World project in a Node.js environment as an example: create a new project folder and open VS Code:



mkdir HelloWorld

cd HelloWorld

code .

Create a new file helloworld.ts in the Explorer.

Write the following TS code; you can see the TS-specific keyword let and the string type annotation:



let message : string = "Hello World";

console.log(message);

Open the Integrated Terminal (shortcut kb(workbench.action.terminal.toggleTerminal)), type tsc helloworld.ts to compile the code; after successful compilation, the JS file helloworld.js is automatically generated.

When Node.js is installed locally, run node helloworld.js to execute the code.

Opening the compiled helloworld.js, you can see that type declarations are removed and let is downleveled to the ES5 var:



var message = "Hello World";

console.log(message);


IntelliSense

IntelliSense provides code auto-completion, hover documentation, and function parameter signature hints, improving coding efficiency and accuracy.

String type code completion

VS Code provides a full set of IntelliSense features for single TS files and TS projects with tsconfig.json configuration.

Hover documentation

Hover over any TS identifier to quickly view the variable/function's type and documentation;
Keyboard shortcut kb(editor.action.showHover) You can manually trigger the hover hint at the cursor position.

lodash function hover information

Function signature hints

When writing a function call, the editor automatically shows the parameter descriptions and highlights the parameter currently being filled in;
Typing the left parenthesis ( or comma , automatically triggers signature hints; the shortcut kb(editor.action.triggerParameterHints) triggers it manually.

capitalize function parameter hint


Code Snippets

In addition to intelligent completion, VS Code has built-in basic TS code snippets that pop up as shortcut templates when you type keywords.

for loop code snippet recommendation

You can install third-party extensions to expand the snippet library or customize TS code snippets; for details, refer to the User-defined Snippets documentation.

Tips: In the configuration file, set editor.snippetSuggestions Set to "none" to disable snippet hints; to keep them, you can configure the snippet display priority:top(pinned to top),bottom(pinned to bottom),inline(mixed with regular hints, sorted alphabetically, the default configuration).


Error and warning hints

The TS language service validates code in real time and automatically flags syntax and logic issues:

Editor error and problem panel

Keyboard shortcut kb(editor.action.marker.nextInFiles) / kb(editor.action.marker.prevInFiles) Jump to the previous or next issue in the current file, with a popup showing details and available quick fixes.

Inline error message

Code navigation

Use navigation features to quickly browse the source of large TS projects:

Through the Command Palette (kb(workbench.action.showCommands)) symbol search for quick navigation:


Code Formatting

VS Code has a built-in TS formatter whose default rules work out of the box.

Pass js/ts.format.* series of settings to customize formatting rules (e.g., braces on a new line). If you don't need the built-in formatter, set:

"js/ts.format.enable": false

to disable it.

To match a team's custom coding standards, you can install a dedicated formatting extension from the marketplace.


Code Refactoring

VS Code comes with common TS refactoring capabilities, such asextract function and extract constant. Select the code to refactor and click the lightbulb icon in the editor margin or use the shortcut kb(editor.action.quickFix) to bring up the refactoring menu.

TS code refactoring panel

For more refactoring rules and custom refactoring shortcuts, refer to the TypeScript Code Refactoring documentation.

Rename Symbol

Select a variable/method and use the shortcut kb(editor.action.rename) to rename all identifiers with the same name in the project with one action.

Identifier renaming


Debugging features

VS Code natively provides comprehensive support for TS debugging, compatible with source maps (SourceMap), supporting breakpoints, variable inspection, call stack tracing, and debug console interaction; for details, refer to the TS Debugging Guide and general debugging documentation.

Front-end web debugging

Debug browser-side TS code using the built-in Chrome/Edge debugging plugin or the Firefox debugging extension.

Back-end Node debugging

Use the built-in debugger to debug Node.js services; the official tutorial helps you quickly get started with debugging Express projects.

Debug variable view


Linters

Linters are used to check for code issues and standardize coding style. VS Code does not include a built-in TS linter; you can install a corresponding extension from the marketplace.

Popular tools ESLint Fully compatible with TS; after installing the ESLint plugin, the editor flags errors in real time and many issues support one-click quick fixes;typescript-eslint The documentation guides ESLint environment configuration for TS projects.