在 Typescript 中使用引号的标准是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25453864/
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
What is the standard for use of quotes in Typescript?
提问by Samantha J T Star
I notice in my application that TsLint is suggesting:
我在我的申请中注意到 TsLint 建议:
static $inject = [
'$http',
'$q',
'$scope',
'configService',
'stateService',
'utilityService'
];
for the above that:
对于上述:
Message 2 TsLint: ' should be "
Is this a suggested standard now for Typescript ?
这是 Typescript 现在的建议标准吗?
采纳答案by Rahul Tripathi
There is no particular standard to use single quotes for characters and double quotes for string but it is suggested to use double quotes for strings and vice versa.
字符使用单引号,字符串使用双引号没有特别的标准,但建议字符串使用双引号,反之亦然。
From the docs:
从文档:
Just like JavaScript, TypeScript also uses the double quote (") or single quote (') to surround string data.
就像 JavaScript 一样,TypeScript 也使用双引号 (") 或单引号 (') 来包围字符串数据。
回答by crowebird
This was the first result in my google search for: "double vs single quotes typescript."
这是我在谷歌搜索中的第一个结果:“双引号与单引号打字稿”。
Considering the accepted answer is a little old (but still valid from the docs) I would like to add this quote from: https://github.com/Microsoft/TypeScript/wiki/Coding-guidelinesupdated on November 27, 2015:
考虑到接受的答案有点旧(但从文档中仍然有效)我想添加以下引文:https: //github.com/Microsoft/TypeScript/wiki/Coding-guidelines更新于 2015 年 11 月 27 日:
Use double quotes for strings.
对字符串使用双引号。
Granted "the code is more what you'd call 'guidelines' than actual rules." :)
授予“代码更像是你所说的'指南'而不是实际规则。” :)
回答by Sa?a ?etkovi?
I would go with single quotes. I pretty much agree with this guy:
我会用单引号。我非常同意这个人:
- Prefer single quotes (') unless escaping.
Reason: More JavaScript teams do this (e.g.airbnb, standard, npm, node, google/angular, facebook/react). Its easier to type (no shift needed on most keyboards).?
Prettier team recommends -
single quotes as welldouble quotes
- 除非转义,否则首选单引号 (')。
原因:更多 JavaScript 团队这样做(例如airbnb、standard、npm、node、google/angular、facebook/react)。它更容易打字(大多数键盘不需要换档)。?
更漂亮的团队推荐——
单引号也是双引号
Also, even dotnet new
templates use single quotes for Angular apps.
此外,甚至dotnet new
模板也对 Angular 应用程序使用单引号。
回答by mkaj
The coding standards document as linked by @crowebird is a good document: https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines
@crowebird 链接的编码标准文档是一个很好的文档:https: //github.com/Microsoft/TypeScript/wiki/Coding-guidelines
I like all the guidelines except for the double quotes - when it comes to using typescript with Angular 2.
我喜欢除了双引号之外的所有指南 - 当涉及到在 Angular 2 中使用打字稿时。
This question isn't about Typescript with Angular 2, but readers may be Angular 2 users. Using single quotes makes for easier reading when marking up html strings in the typescript.
这个问题与 Angular 2 的 Typescript 无关,但读者可能是 Angular 2 用户。在打字稿中标记 html 字符串时,使用单引号更容易阅读。
Take the following example:
以下面的例子为例:
@Component({
...,
template: '<div class="some-class-name"></div>'
})
But if you are using double quotes you have to escape the double quotes:
但是,如果您使用双引号,则必须转义双引号:
@Component({
...,
template: "<div class=\"some-class-name\"></div>"
})
The first option is preferable. Most of the Angular 2 demos use single quotes.
第一种选择更可取。大多数 Angular 2 演示都使用单引号。
回答by Seth
Since it seems like there is no hard and fast answer, what's consistent across languages?
既然似乎没有硬性的答案,那么跨语言的一致性是什么?
Bash/Powershell/Ruby: " enables interpolation and escape sequences. ' means the string is exactly as it is typed.
Bash/Powershell/Ruby: " 启用插值和转义序列。' 表示字符串与输入的字符串完全一样。
C-stylelanguages (Java, C#, C++ etc): " is a string while ' for single characters.
C 风格的语言(Java、C#、C++ 等):" 是一个字符串,而 ' 表示单个字符。
Python/Javascript: no difference. If a string needs to contain ", you might delimit it with ' and vice versa.
Python/Javascript:没有区别。如果字符串需要包含 ",您可以用 ' 分隔它,反之亦然。
JSON: double quotes only. This is the tilting argument.
JSON:仅双引号。这是倾斜的论点。
Across languages, single quotes imply a lack of escape sequences and interpolation.
在各种语言中,单引号意味着缺少转义序列和插值。
Typescript has backwards compatibility for ` (back-tick) strings so my preference is to use " (double quote) for non-escaped strings, generally free of white-space and in the following character set:
Typescript 对 `(反引号)字符串具有向后兼容性,因此我更喜欢对非转义字符串使用 "(双引号),通常没有空格并使用以下字符集:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-
or [\w\d-]*
in many regex dialects. This means you can copy pasta object literals into JSON and vice versa. Quite useful in practice for little investigations, tests, etc.
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-
或[\w\d-]*
在许多正则表达式方言中。这意味着您可以将面食对象文字复制到 JSON 中,反之亦然。在实践中对于小调查、测试等非常有用。
For everything else, ` (back-ticks) since it cuts down on escape sequences and enables interpolation.
对于其他所有内容,`(反引号)因为它减少了转义序列并启用了插值。
Note: I am not an advocate of JSON, it just seems inescapable these days ;)
注意:我不是 JSON 的拥护者,这几天似乎不可避免;)
回答by JGFMK
If you are going to be embedding template strings, then back ticks:
如果您要嵌入模板字符串,请回勾:
`
Straight from here
直接从这里
const lyrics = 'Never gonna give you up';
const html = `<div>${lyrics}</div>`;
I personally prefer single quotes because ''
vs ""
is slightly less confusing to eyeball when dealing with empty string constant.
我个人更喜欢单引号,因为在处理空字符串常量时''
vs""
对眼球的混淆稍微少一些。
But it's ok to override that rule if you need embed a single quote. Use the double quotes to wrapper things to avoid escaping headaches.
但是如果您需要嵌入单引号,则可以覆盖该规则。使用双引号来包装事物以避免逃避麻烦。
Flexibility is the key.
灵活性是关键。
回答by Pierrie Mostert
Locate your tslint.json file and change the following json setting(s)
找到您的 tslint.json 文件并更改以下 json 设置
"quotemark": [
[
true,
"double"
],
[
true,
"single"
]
],
This allows for single and double quote marks in your ts files.
这允许在 ts 文件中使用单引号和双引号。
When using Visual Studio a restart of Visual Studio might be required.
使用 Visual Studio 时,可能需要重新启动 Visual Studio。