typescript Visual Studio Code - 调整导入报价设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42952453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:38:09  来源:igfitidea点击:

Visual Studio Code - Adjust import quotation setting

typescriptvisual-studio-code

提问by Boris van Katwijk

When working in TypeScript in Visual Studio Code, the import suggestion on a type (triggered by space + period) will generate an import using double quotes.

在 Visual Studio Code 中使用 TypeScript 时,类型的导入建议(由空格 + 句点触发)将使用双引号生成导入。

Our TypeScript linter verifies that single quotes are used where possible.

我们的 TypeScript linter 会验证是否尽可能使用单引号。

As you can see below, the suggestion has double quotes ("@angular/...") Import suggestion with double quotes

正如你在下面看到的,建议有双引号 ("@angular/...") 带双引号的导入建议

How can I adjust the setting of the import?

如何调整导入设置?

采纳答案by Sebastian Sebald

As of VSCode 1.10, this is (sadly) not possible yet. But is an issue for a lof of the users as it seems. The VSCode theme is aware of this issue and you can follow this to know when it is implemented: https://github.com/Microsoft/TypeScript/issues/13270

从 VSCode 1.10 开始,这(遗憾的是)还不可能。但是对于很多用户来说似乎是个问题。VSCode 主题知道这个问题,你可以按照这个来了解它何时实施:https: //github.com/Microsoft/TypeScript/issues/13270



Update June 2018

2018 年 6 月更新

Since VSCode 1.24 (June 2018) there is an option for this!

自 VSCode 1.24(2018 年 6 月)以来,有一个选项!

"typescript.preferences.quoteStyle": "single"

"typescript.preferences.quoteStyle": "single"

For more info see:

有关更多信息,请参阅:

https://code.visualstudio.com/updates/v1_24#_preferences-for-auto-imports-and-generated-code

https://code.visualstudio.com/updates/v1_24#_preferences-for-auto-imports-and-generated-code

回答by abdllhbyrktr

You can also configure the below line in your vscode user settings to adjust this setting.

您还可以在 vscode 用户设置中配置以下行来调整此设置。

"prettier.singleQuote": true

回答by Joel Jacquez

I fixed that using Editor config, open your .editorconfigfile in your project root directory (if you don't have, create that file) and add this line after the [*]

我使用Editor config修复了该问题,在项目根目录中打开.editorconfig文件(如果没有,请创建该文件)并在[*]之后添加这一行

[*]
...
quote_type = single

In the wikiyou can see the complete list of properties.

wiki 中,您可以看到完整的属性列表。

回答by cdoremus

As of VS Code 1.21.1 you need to edit

从 VS Code 1.21.1 开始,您需要编辑

/usr/share/code/resources/app/extensions/typescript-basics/snippets/typescript.json

/usr/share/code/resources/app/extensions/typescript-basics/snippets/typescript.json

In Windows

在 Windows 中

/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/typescript-basics/snippets/typescript.json.

/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/typescript-basics/snippets/typescript.json

In Windows 10 (vscode version 1.30.* (user setup) later)

在 Windows 10 中(vscode 版本 1.30.*(用户设置)之后)

*C:\Users\<yourusername>\AppData\Local\Programs\Microsoft VS Code\resources\app\extensions\typescript-basics\snippets\typescript.json

*C:\Users\<您的用户名>\AppData\Local\Programs\Microsoft VS Code\resources\app\extensions\typescript-basics\snippets\typescript.json

In the 'Import external module' section of that file make the body array property to be the value "import { $0 } from '${1:module}';"The section will then look like this:

在该文件的“导入外部模块”部分中,将 body 数组属性设为值"import { $0 } from '${1:module}';",然后该部分将如下所示:

"Import external module.": {
    "prefix": "import statement",
    "body": [
        "import { 
"typescript.preferences.quoteStyle": "single",
"javascript.preferences.quoteStyle": "single"
} from '${1:module}';" ], "description": "Import external module." },

回答by Boris van Katwijk

An alternative which does support this configuration is TypeScript Toolbox.

支持此配置的替代方法是TypeScript Toolbox

It is configurable by setting genGetSet.pathStringDelimiter, which already has the single-quote import as default.

它可以通过设置进行配置,genGetSet.pathStringDelimiter默认情况下已经将单引号导入。

回答by Wagner da Silva

Go to "File > Preferences > Settings" and then add this under user settings:

转到“文件>首选项>设置”,然后在用户设置下添加:

"prettier.singleQuote": true

回答by Trevor Daniels

As of TypeScript 2.5, the first import or export statement in the file will be scanned to determine if single or double quotes are used when using import suggestions.

从 TypeScript 2.5 开始,将扫描文件中的第一个导入或导出语句,以确定在使用导入建议时是使用单引号还是双引号。

https://github.com/Microsoft/TypeScript/pull/17750

https://github.com/Microsoft/TypeScript/pull/17750

回答by anirban8611

You can also configure the below line in your vscode user settings to allow single quote in string.

您还可以在 vscode 用户设置中配置以下行以允许字符串中的单引号。

Go to Preferences > User Settings

转到首选项 > 用户设置

"tslint.autoFixOnSave": true

This will allow single quote in String. Otherwise, if you manually change all double quotes to single quotes it will revert back while saving. Also, add

这将允许在字符串中使用单引号。否则,如果您手动将所有双引号更改为单引号,它将在保存时恢复。另外,添加

##代码##

to auto-fix while saving.

保存时自动修复。

回答by Andrei Sinitson

This is already implemented (as mentioned in another reply)! But you are probably not on the latest version of TypeScript yet.

这已经实施了(如另一个回复中所述)!但是您可能还没有使用最新版本的 TypeScript。

Solution is simple:

解决方法很简单:

Click TypeScript version number (for example 2.3.4) between "TypeScript" and a little smiley face in the lower right corner. Then switch to Visual Studio Code built-in version (2.5.3 at this moment).

单击“TypeScript”和右下角的小笑脸之间的 TypeScript 版本号(例如 2.3.4)。然后切换到 Visual Studio Code 内置版本(此时为 2.5.3)。

After this Visual Studio code will infer import quote style by looking at the first import statement. Note that a little popup label will still show double-quotes anyway.

在此之后,Visual Studio 代码将通过查看第一个导入语句推断导入引用样式。请注意,无论如何,一个小的弹出标签仍然会显示双引号。

Bug report

错误报告

Relevant pull request:

相关拉取请求

This adds the ability to determine whether to use single or double quotes for new imports added via code fixes. When a new import is added, we scan the top-most statements of the source file for existing import or export declarations with module specifiers. We then use the quote style of the first one we find. If there are no existing imports in the file we fall back to using double quotes.

这增加了确定对通过代码修复添加的新导入使用单引号还是双引号的能力。添加新的导入时,我们会扫描源文件的最顶层语句,以查找带有模块说明符的现有导入或导出声明。然后我们使用我们找到的第一个引用样式。如果文件中没有现有的导入,我们将回退到使用双引号。

回答by Varun Sukheja

Above solutions did not work for me

以上解决方案对我不起作用

So here is my work around, in you use vscode, "tslint.autoFixOnSave": true in your settings.jsonwill automatically fix these import quotations when you save the file.

所以这是我的解决方法,当您使用 vscode,"tslint.autoFixOnSave": true 时,您settings.json将在保存文件时自动修复这些导入引用。