TypeScript with Visual Studio Code

Visual Studio Code (VS Code) has become the preferred editor for TypeScript developers, offering built-in support and powerful features that make working with TypeScript a breeze. The tight integration between VS Code and TypeScript provides instant feedback, intelligent code completion, and robust debugging capabilities right out of the box. Whether you're new to TypeScript or an experienced developer, mastering these tools will significantly boost your productivity and code quality.

Setting Up TypeScript in VS Code

Getting started with TypeScript in VS Code is simple, but proper setup ensures you get the most from your development environment.

Initial Configuration

1. Install VS Code from the official website 2. Add TypeScript support:

bash
1
             npm install -g typescript
        

3. Create a tsconfig.json in your project root:

bash
1
             tsc --init
        

Recommended Settings

Add these to your VS Code settings.json (File > Preferences > Settings):

json
1
2
3
4
5
6
7
8
9
10
          {
  "typescript.tsdk": "node_modules/typescript/lib",
  "editor.formatOnSave": true,
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true,
    "source.organizeImports": true
  },
  "typescript.updateImportsOnFileMove.enabled": "always",
  "javascript.updateImportsOnFileMove.enabled": "always"
}
        

Workspace Configuration

For multi-project workspaces:

1. Create a .vscode folder in your project 2. Add these configuration files:

launch.json (for debugging):

json
1
2
3
4
5
6
7
8
9
10
11
12
13
          {
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Debug Current TS File",
      "program": "${file}",
      "preLaunchTask": "tsc: build - tsconfig.json",
      "outFiles": ["${workspaceFolder}/dist/**/*.js"]
    }
  ]
}
        

tasks.json (for build tasks):

json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
          {
  "version": "2.0.0",
  "tasks": [
    {
      "type": "typescript",
      "tsconfig": "tsconfig.json",
      "problemMatcher": ["$tsc"],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "label": "tsc: build - tsconfig.json"
    }
  ]
}
        

Using TypeScript Features in VS Code

VS Code's TypeScript integration goes far beyond basic syntax highlighting. Here are the powerful features you should be using:

Intelligent Code Completion (IntelliSense)

1. Type-Aware Suggestions:

  • VS Code suggests relevant methods and properties based on types
  • Press Ctrl+Space to trigger completions manually

2. Quick Info:

  • Hover over any identifier to see its type definition
  • Includes JSDoc comments if available

3. Signature Help:

  • When calling functions, shows parameter types and documentation
  • Trigger with Ctrl+Shift+Space

Advanced Navigation

1. Go to Definition (F12):

  • Jump directly to where a symbol is defined

2. Peek Definition (Alt+F12):

  • View definition without leaving your current file

3. Find All References (Shift+F12):

  • See everywhere a symbol is used in your project

Powerful Refactoring

1. Rename Symbol (F2):

  • Change a name everywhere it's used in your project

2. Extract to Function/Method/Constant:

  • Select code > Right click > Refactor
  • Creates properly typed functions automatically

3. Organize Imports:

  • Automatically removes unused imports
  • Sorts imports alphabetically

Integrated Debugging

1. Setting Breakpoints:

  • Click in the gutter next to line numbers
  • Works in both .ts and generated .js files

2. Debug Configuration:

json
1
2
3
4
5
6
7
8
9
             {
     "type": "node",
     "request": "launch",
     "name": "Debug TS Node",
     "runtimeExecutable": "ts-node",
     "args": ["${relativeFile}"],
     "cwd": "${workspaceFolder}",
     "protocol": "inspector"
   }
        

3. Debugging Features:

  • Hover debugging (see variable values by hovering)
  • Watch expressions
  • Call stack inspection

Type Checking in Real-Time

1. Error Squiggles:

  • Red underlines appear as you type invalid code

2. Problems Panel (Ctrl+Shift+M):

  • View all TypeScript errors in one place

3. Quick Fixes (Ctrl+.):

  • Apply suggested fixes for common type errors

Best Extensions for TypeScript Development

While VS Code has excellent built-in TypeScript support, these extensions take your development experience to the next level:

Essential Extensions

1. ESLint:

  • Integrates ESLint with TypeScript support
  • Shows linting errors directly in the editor

2. Prettier:

  • Opinionated code formatter
  • Works seamlessly with TypeScript files

3. Path IntelliSense:

  • Autocompletes file paths in import statements

4. TypeScript Importer:

  • Automatically adds imports when using types
  • Organizes imports intelligently

Productivity Boosters

1. TabNine:

  • AI-powered code completions
  • Learns from your codebase

2. Code Spell Checker:

  • Catches typos in identifiers and comments

3. Error Lens:

  • Shows error messages inline with your code

4. REST Client:

  • Test APIs directly from VS Code
  • Great for TypeScript API development

Specialized Tools

1. GraphQL:

  • Syntax highlighting and validation
  • Works with TypeScript GraphQL servers

2. Docker:

  • Manage containers for TypeScript microservices

3. Jest Runner:

  • Run and debug TypeScript tests directly

4. Live Share:

  • Collaborative TypeScript development

Configuration Tips

Add these to your VS Code settings for optimal TypeScript extension setup:

json
1
2
3
4
5
6
7
8
9
10
          {
  "eslint.validate": ["typescript", "typescriptreact"],
  "prettier.jsxSingleQuote": true,
  "prettier.semi": false,
  "javascript.preferences.quoteStyle": "single",
  "typescript.preferences.quoteStyle": "single",
  "editor.tabSize": 2,
  "typescript.suggest.autoImports": true,
  "typescript.updateImportsOnFileMove.enabled": "prompt"
}
        

Workflow Optimization

1. Keybindings:

  • Add custom shortcuts for frequent TypeScript actions

Example keybindings.json:

json
1
2
3
4
5
6
7
8
9
10
11
12
             [
     {
       "key": "ctrl+shift+r",
       "command": "typescript.restartTsServer",
       "when": "editorTextFocus"
     },
     {
       "key": "ctrl+alt+d",
       "command": "editor.action.peekDefinition",
       "when": "editorTextFocus"
     }
   ]
        

2. Snippets:

  • Create custom TypeScript code snippets

Example snippets.json:

json
1
2
3
4
5
6
7
8
9
10
11
             {
     "TS Interface": {
       "prefix": "tinterface",
       "body": [
         "interface ${1:Name} {",
         "\t${2:property}: ${3:type};",
         "}"
       ],
       "description": "Create a TypeScript interface"
     }
   }
        

3. Task Automation:

  • Set up build tasks that run automatically

By combining VS Code's built-in TypeScript support with these carefully selected extensions and configurations, you'll create a development environment that helps you write better TypeScript code faster and with fewer errors. The intelligent feedback loop between writing code and seeing type information will soon become an indispensable part of your workflow.