javascript - VS Code 强制使用分号,但 Angular TypeScript 示例的使用不一致?

标签 javascript angular typescript visual-studio-code tslint

我现在正在学习 Microsoft Angular 基础类(class),我发现他们的示例中缺少分号,例如:

gitSearch = (query: string): Promise<GitSearch> => {
    let promise = new Promise<GitSearch>((resolve, reject) => {
        if (this.cachedValues[query]) {
            resolve(this.cachedValues[query])
        }
        else {
            this.http.get('https://api.github.com/search/repositories?q=' + query)
            .toPromise()
            .then( (response) => {
                resolve(response as GitSearch)
            }, (error) => {
                reject(error);
            })
        }
    })
    return promise;
  }

请注意,他们在 resolve.thisCachedValues[query]) 之后不使用分号。但 VS Code 在该行给了我 TSLint 警告 'Missing semicolon' 。如果我省略分号会出现问题吗?

最佳答案

JavaScript 有一个称为分号插入的语法功能。由于 Typescript 是 JavaScript 的严格超集,因此分号插入也适用于 Typescript。这意味着在大多数情况下,不需要分号。下面详细介绍了异常(exception)情况。

来自 JavaScript Semicolon Insertion: Everything you need to know

[T]he [ECMAScript] specification prose then gives rules which describe how the actual parsing differs from the formal grammar. These are described as though semicolons are inserted into the token stream while parsing, though this is just a specification convenience; in practice, parsers do not need to generate fake semicolon tokens, but can instead regard semicolons as optional in specific places in the grammar (for an example see this parser expression grammar for ECMAScript, particularly the Statement, EOS, EOSnoLB, and SnoLB rules). Where the specification says that a semicolon is inserted, this simply means that the statement currently being parsed is ended.

哪里分号是可选的,哪里是必需的?

  • Firstly, a semicolon is optional only where there is a line break, a closing brace, or the end of the program.
  • Semicolons are not optional between statements appearing on the same line.
  • A semicolon is not implied at the end of a line if the first token of the subsequent line can be parsed as part of the same statement.

TSLint Semicolon Rule

TSLint(由 VS Code 使用)支持 semicolon rule 。在tslint.json中,分号规则可以配置为:

  • "always" enforces semicolons at the end of every statement.
  • "never" disallows semicolons at the end of every statement except for when they are necessary.

TSLint 分号规则示例

"semicolon": [true, "never"]
"semicolon": [true, "always"]
"semicolon": [true, "always", "ignore-interfaces"]

TSLint being deprecated in 2019

TSLint 将于 2019 年弃用并迁移到 ESLint,这将成为 TypeScript 和 JavaScript 的标准 linter。

该工具tslint-to-eslint-config帮助将 TSLint 配置转换为等效的 ESLint 配置。

用法

npx tslint-to-eslint-config

关于javascript - VS Code 强制使用分号,但 Angular TypeScript 示例的使用不一致?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57726221/

相关文章:

javascript - 如何在jquery日期选择器字段附近创建前一天和后一天的链接

javascript - 如何从异步调用返回响应?

javascript - 类型 'url' 上不存在属性类型 'Event'

angular - ionic 存储在 chrome 浏览器中不起作用

reactjs - Webpack css-loader 失败 : "You may need an appropriate loader to handle this file type."

javascript - ES6中的静态导入是什么意思?

javascript - 如何更改剑道 ui 网格过滤器上的图标或图标颜色?

angular - typescript :对算术运算进行编码

javascript - Angular 多步形式路由

angular - 类型错误 : Cannot read property 'flags' of undefined