【问题标题】:Add space between variable name and colon in Visual Studio Code + Typescript在 Visual Studio Code + Typescript 中的变量名和冒号之间添加空格
【发布时间】:2021-02-08 12:58:51
【问题描述】:

作为一个多年来专门使用 UnityScript 的人,我非常习惯于这样定义变量:

var name : string = 'Bob';

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

var name: string = 'Bob';

如何让它在变量名和冒号之间添加空格?

【问题讨论】:

    标签: typescript visual-studio-code


    【解决方案1】:

    遗憾的是,VS Code 本身没有选择;但是,您可以将 ESLint plugin for VS Code 设置为 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,
            },
        ],
      }
    }
    

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

    More information on the TypeScript ESLint, including installation

    【讨论】:

      【解决方案2】:

      在 Visual Studio 代码中安装 typescript(TSLint) 的插件。

      现在是修复空间或错误问题的示例。

      警告或错误 解决方案

      将鼠标悬停在 设备(警告)上并点击修复,您会看到类似的选项 -> 现在选择 Fix-missing whitespaceFix all-missing whitespace 选项

      同样的方法可以解决错误等其他问题

      【讨论】:

      • 这是否将var name: string 修复为var name : string
      • 请注意:“TSLint 已被弃用,取而代之的是 ESLint。”
      猜你喜欢
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 2019-05-22
      相关资源
      最近更新 更多