Essential VS Code extensions for TypeScript
Tags: typescript
Published: 2022-04-15 17:49:39

**Written by [Harsh Patel](https://blog.logrocket.com/author/harshpatel/)✏️**

TypeScript has classes, interfaces, and modules that can be useful to define types. It’s always a good practice to use TypeScript with JavaScript.

If you’re using VS Code for writing code, then it has rich extensions to help configure and use TypeScript easily. Here are some essentials VS Code extensions for TypeScript.

## **TypeScript ESLint**

[TS ESLint](https://github.com/typescript-eslint/typescript-eslint) is a must-have VS Code extension when you’re working with TypeScript. TypeScript ESLint is used to lint your code and check basic code guidelines. By using it, you’re ensuring basic code quality and integrity.

### Why linting is used?

You know that JS is an interpreter language, so each error is encountered at runtime only because there’s no compilation phase. You need to make sure that your code does not throw any errors in the execution phase.

ESLint does a very good job at catching errors. Apart from that, it’s necessary to maintain code conventions when your code is used by others or when your project has multiple contributors. Generally, linters are used to identify basic bugs such as scope errors and undeclared variable assignments.

### Setting up VS Code ESLint

![Setting Up VS Code ESLint](https://blog.logrocket.com/wp-content/uploads/2022/04/setting-up-vs-code-eslint.png)

As soon as you [install ESLint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint), it’s available in VS Code. It works if your project has a local ESLint file. If not found, then it will check for a global config file in the system. In the coming step, you can find how to configure ESLint in your existing or new project.

After installation, you can change default ES Lint settings as well. Go to **Code** > **Preferences** > **Settings** and then directly type **ESLint** in the search bar or find ESLint under the **Extensions** menu and click on it. Make sure you’re in a **User** tab. Now you can see all settings provided by VS Code. There’s also a provision to modify VS Code’s default `setting.json`.

### How do you configure TypeScript ESLint?

Install base dependency that you need throughout the project by running the given command:

```typescript
> npm install --save-dev eslint typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin
```

Now you need to configure your lint file. There are two options available for that: either configure it manually or create a lint file in the root directly. Let’s understand how to configure it manually. Run any one command from these two:

```typescript
> eslint --init 

> npm init @eslint/config
```

After that, you need to choose the appropriate option for the asked questions. Let’s start with the first step; choose according to your requirement:

![Checking Syntax](https://blog.logrocket.com/wp-content/uploads/2022/04/checking-syntax.png)

The best practice is to go with the third one so you can analyze code syntax defects, and it will be better for a large scale.

Moving to the second step, choose an option as per your project guidelines:

![Specifying Modules](https://blog.logrocket.com/wp-content/uploads/2022/04/specifying-modules.png)

The next question is for your project framework:

![Specifying Framework](https://blog.logrocket.com/wp-content/uploads/2022/04/specifying-framework.png)

After this, it will ask for usage of TypeScript:

![Typescript](https://blog.logrocket.com/wp-content/uploads/2022/04/typescript.png)

Of course it’s a TypeScript project, so you need to click on **Yes**. Because TypeScript and ESLint are useful for both frontend and backend, the next step is identifying which your project is for:

![Identifying Project](https://blog.logrocket.com/wp-content/uploads/2022/04/identifying-project.png)

Now choose a style guide:

![Style Guide](https://blog.logrocket.com/wp-content/uploads/2022/04/style-guide.png)

It is purely based on your project requirements. You can go with any option. The next question is about config file format. Choose a format in which you’re comfortable:

![Choosing Format](https://blog.logrocket.com/wp-content/uploads/2022/04/choosing-format.png)

Then it will ask you to install dependency based on your selection; you just need to select the **Yes** option.

![Installing Dependency](https://blog.logrocket.com/wp-content/uploads/2022/04/installing-dependency.png)

After successfully installing dependencies, you can see the `.eslintrc.js` (here, the extension is `.js` because I choose JavaScript in the config file format) file in your project root with your selected options. Initially, there’s an empty object for rules. For TypeScript support, you need to add a rule like `react/jsx-filename-extension': [1, { extensions: ['.ts'] }]`.

In the extensions section, you can add file extensions. As an example, for React, `.tsx` is also required. Further, you can add whichever rules you want from the [available rules](https://eslint.org/docs/rules/).

The second way is to create JavaScript, JSON, or YAML file in your project root and write it from scratch. Here’s an example of a `.js` file:

```js
//.eslintrc.js
module.exports = {
    rules: {
        // Write rules here
    }
};
```

If you open any file, it will have a colorful underline if it failed to match the TypeScript rules. If you want to know more about an error or warning, hover on the underlined code and you can see a pop-up message. There are two buttons: one is to see more details about an issue and another is to quickly fix it. The **Quick fix** option is only available for some rules.

![Quick Fix](https://blog.logrocket.com/wp-content/uploads/2022/04/quick-fix.png)

You can also disable ES Lint for some specific blocks of code by wrapping them in `/* eslint-disable */` and `/* eslint-enable */`. If it’s a single line, just add `// eslint-disable-line` at the end of your line. This is useful when you want to disable linting.

Another good feature provided by VS Code is it gives an option to automatically fix an issue when you save a file. For that, you need to change settings. Go to **Code** > **Preferences** > **Settings** and type ES Lint in the search bar.

Once search results appear, click on the **ESLint** form extensions. After that, choose the **Workspace** tab that’s just below the search box. On the right side, you can see an option like **Edit in settings.json**. When you click on it, it will navigate you to your workspace’s ESLint settings file. There you need to copy a couple of lines written below:

```typescript
{
    "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true
    },
    "eslint.validate": ["typescript", "typescriptreact"]
}
```

With this setting, VS Code will correct basic errors on save.

### ESLint configs provided by leading companies

There are three things that should be handled when you’re writing code: architecture, styling, and maintainability. Thinking about the long term, it’s better to follow ESLint best practices.

Your code needs to be refactored if it’s not efficient each time. So it’s better to take care of your code at the beginning. Code quality should be high and consistent. Some of the best ESLint configs are provided by leading IT firms:

*   [Airbnb](https://github.com/airbnb/javascript/tree/master/packages/eslint-config-airbnb)
*   [ESLint](https://github.com/eslint/eslint/tree/master/packages/eslint-config-eslint)
*   [Facebook](https://www.npmjs.com/package/eslint-config-fbjs)
*   [Google](https://github.com/google/eslint-config-google)
*   [React](https://github.com/facebook/create-react-app/tree/master/packages/eslint-config-react-app)
*   [Shopify](https://github.com/Shopify/web-foundation/blob/main/packages/eslint-plugin/README.md)

If these aren’t for you, there are tons of config available. Or you can make your own config and use that in the project. TS also provides a facility to avoid certain rules or to add a custom rule.

### Running ESLint manually

Suppose you disabled linting in a project but you want to check in on a specific file. You simply run:

```typescript
eslint fileName.ts
```

If a file contains errors, then each error will be listed down sequentially.

![Sequential List Of Errors](https://blog.logrocket.com/wp-content/uploads/2022/04/sequential-list-errors.png)

## TypeScript Toolbox

![Typescript Toolbox](https://blog.logrocket.com/wp-content/uploads/2022/04/typescript-toolbox.png)

[The TypeScript Toolbox](https://marketplace.visualstudio.com/items?itemName=DSKWRK.vscode-generate-getter-setter) is best when you want to improve already existing VS Code generation extensions. Mainly it is used for code generation as the overview suggests. Along with that, it is also used for cases like the following:

*   Automatically adding missing imports and removing unused ones. ![Adding Imports](https://blog.logrocket.com/wp-content/uploads/2022/04/adding-imports.png) When you click on the yellow bulb icon, then you can see a popup regarding missing imports. Once you click on it, it will be added to your code.
*   Autogenerate getters/setters with appropriate namings. ![Autogenerate Getters And Setters](https://blog.logrocket.com/wp-content/uploads/2022/04/autogenerate-getters-setters.png) As you can see, all getter and setter methods are properly generated. In a few steps, it’s possible to generate all methods; it saved lots of time.
*   When working on classes, it auto-generates constructors. ![Autogenerate Constructors](https://blog.logrocket.com/wp-content/uploads/2022/04/autogenerate-constructors.png) In this code, the constructor is autogenerated using TypeScript Toolbox.

## TypeScript Importer

![Typescript Importer](https://blog.logrocket.com/wp-content/uploads/2022/04/typescript-importer.png)

It’s necessary to save time when you want to import each and every external module used in a file. TS importer is the best extension when you want to add an appropriate import statement in your file without writing it.

[![Using TS Importer](https://blog.logrocket.com/wp-content/uploads/2022/04/using-ts-importer.gif)](https://blog.logrocket.com/essential-vs-code-extensions-typescript/using-ts-importer-2/)

You can see from the video how you can add the import. Behind the screen, it searches the definition in the existing workspace, and if it’s available then it is shown in the dropdown and just by selection of it will be imported.

## Ponicode

![Ponicode](https://paper-attachments.dropbox.com/s_C836D12FB546363174224941C3F6CA9EFD3E9BB00774B1845A39B4A82A2A5313_1645719866329_ponicod.png)

It’s always a good practice to test individual units of a project. A unit test will provide useful insights about individual units of a project, and if you’re using TypeScript and VS Code, [Ponicode](https://marketplace.visualstudio.com/items?itemName=ponicode.ponicode) is there to ease your unit testing task.

Ponicode identifies which functions need to be tested and generates unit test cases using AI, thus you can run test cases and check outputs. As a result, the code will be bug-free.

Ponicode provides a nice GUI to write test suits and a respective test file will be generated with properly written test cases. It also recommends best test cases.

Apart from that, Ponicode can generate an end-to-end test case in just one click so you can achieve high coverage.

[![Ponicode Demo](https://blog.logrocket.com/wp-content/uploads/2022/04/ponicode-demo.gif)](https://blog.logrocket.com/essential-vs-code-extensions-typescript/ponicode-demo-2/)

You can see it quickly generated test cases for one simple function. You just need to select or add test data from GUI and the rest will be handled by Ponicode.

## CodeMetrics

![CodeMetrics](https://blog.logrocket.com/wp-content/uploads/2022/04/codemetrics.png)

Code complexity does matter. Sometimes logic can be simple, but code is written with many unnecessary conditions or with complex logic, which results in reflecting. Reflecting impacts time and effort. Therefore, tools like [CodeMetric](https://marketplace.visualstudio.com/items?itemName=kisstkondoros.vscode-codemetrics) are useful. CodeMagic identifies code complexity based on its predefined conditions.

![CodeMagic](https://blog.logrocket.com/wp-content/uploads/2022/04/codemagic.png)

At the start of a function, you can see the complexity number; it is the default provided by CodeMetric, but it can be customized. Just edit the settings of the `codemetrics.basics` file. It’s pretty useful when you want to write optimized code.

## Move TS

![MoveTS](https://blog.logrocket.com/wp-content/uploads/2022/04/movets.png)

Files often need to be moved from one directory to another. It’s hard to keep track of where that file is used if a project contains many files. If you don’t use [Move TS](https://marketplace.visualstudio.com/items?itemName=stringham.move-ts), then you need to search for it and then traverse into each file and manually change the import path. Move TS makes that work easy. If you change the file location, then it will change all relative used paths automatically. Pretty simple, right?

## Conclusion

VS Code is a widely used IDE for programming. Having multiple TypeScript extensions would be the cherry on top.

Extensions will help you write elegant code and save time by automating some tasks. And VS Code provides good support to their users. If you’re a TS developer, then you should use these extensions to make your code beautiful and bug-free.

---

## Writing a lot of TypeScript? [Watch the recording](https://blog.logrocket.com/typescript-meetup-recap/?utm_source=social&utm_medium=organic&utm_campaign=21Q3_WB_TS-Tech-Meetup0930#utm_source=social&utm_medium=organic&utm_campaign=21Q3_WB_TS-Tech-Meetup0930) of our recent TypeScript meetup to learn about writing more readable code.

[![Write More Readable Code with TypeScript 4.4](https://blog.logrocket.com/wp-content/uploads/2021/07/typescript-4-4-more-readable-code.png)](https://blog.logrocket.com/typescript-meetup-recap/?utm_source=social&utm_medium=organic&utm_campaign=21Q3_WB_TS-Tech-Meetup0930#utm_source=social&utm_medium=organic&utm_campaign=21Q3_WB_TS-Tech-Meetup0930)

TypeScript brings type safety to JavaScript. There can be a tension between type safety and readable code. Watch the recording for a deep dive on some new features of TypeScript 4.4.