3

As someone who used UnityScript exclusively for many years, I became very used to defining variables like this:

var name : string = 'Bob';

On a Node-based project, I just started using Typescript, however I noticed that VS Code seems to want to do this instead:

var name: string = 'Bob';

How can I make it add the space between the variable name and the colon?

2 Answers 2

5

Sadly there's no option is VS Code itself; however, you can make the ESLint plugin for VS Code your default formatter for TypeScript and use the type-annotation-spacing rule with the following configuration:

.eslintrc.js

module.exports = {
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint"],
  "rules": {
    "@typescript-eslint/type-annotation-spacing": [
        "error",
        {
            "before": true,
            "after": true,
        },
    ],
  }
}

There doesn't seem to be a method to make Prettier or VS Code enforce the same rules, but I can recommend using ESLint for getting into the nitty-gritty of formating!

More information on the TypeScript ESLint, including installation

Sign up to request clarification or add additional context in comments.

Comments

0

Install plugin of typescript(TSLint) in visual studio code.

Now Example to fix space or error issues.

Warning or error enter image description here Solution

Hover on device(warning) and click on Fix and you see options like -> now select Fix-missing whitespace and Fix all-missing whitespace enter image description here options enter image description here

Same way you can fix other issues like errors

2 Comments

Does this fix var name: string to var name : string?
Please be warned: "TSLint has been deprecated in favor of ESLint."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.