简体   繁体   English

在 Visual Studio Code + Typescript 中的变量名和冒号之间添加空格

[英]Add space between variable name and colon in Visual Studio Code + Typescript

As someone who used UnityScript exclusively for many years, I became very used to defining variables like this:作为一个多年来专门使用 UnityScript 的人,我非常习惯于这样定义变量:

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:在一个基于节点的项目中,我刚开始使用 Typescript,但是我注意到 VS Code 似乎想要这样做:

var name: string = 'Bob';

How can I make it add the space between the variable name and the colon?如何让它在变量名和冒号之间添加空格?

Sadly there's no option is VS Code itself;可悲的是,没有选择是 VS Code 本身。 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:但是,您可以将VS Code 的 ESLint 插件设置为 TypeScript 的默认格式化程序,并使用带有以下配置的type-annotation-spacing规则:

.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!似乎没有一种方法可以让 Prettier 或 VS Code 强制执行相同的规则,但我可以推荐使用 ESLint 来深入了解格式化的本质!

More information on the TypeScript ESLint, including installation 有关 TypeScript ESLint 的更多信息,包括安装

Install plugin of typescript( TSLint ) in visual studio code.在 Visual Studio 代码中安装 typescript ( TSLint ) 插件。

Now Example to fix space or error issues.现在修复空间或错误问题的示例。

Warning or error警告或错误在此处输入图像描述 Solution解决方案

Hover on device (warning) and click on Fix and you see options like -> now select Fix-missing whitespace and Fix all-missing whitespace Hover 在设备上(警告)并单击修复,您会看到类似的选项 -> 现在 select修复缺失的空白修复所有缺失的空白在此处输入图像描述 options选项在此处输入图像描述

Same way you can fix other issues like errors同样的方式你可以解决其他问题,如错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM