我在 tsconfig 中使用这些规则是为了在 vscode 中突出显示未使用的变量和导入。
"noUnusedLocals": true, /* Report errors on unused locals. */
"noUnusedParameters": true, /* Report errors on unused parameters. */
Run Code Online (Sandbox Code Playgroud)
但是我希望这些规则不会阻止编译。那可能吗 ?
我有一个由 angular-cli 生成的组件,.ts 文件的结果代码有:
ngOnInit() {
}
Run Code Online (Sandbox Code Playgroud)
代替
ngOnInit(): void {
}
Run Code Online (Sandbox Code Playgroud)
我的 tslint 被配置为总是希望存在 typedef,但 cli 生成的代码没有 typedef - 这是我的问题。
有没有办法将 cli 配置为始终设置所需的 typedef,或者我唯一的选择是禁用此 tslint 检查或手动修复它?
无状态 React 组件应该以PascalCase命名,以便 React 可以区分原生元素和组件。Typescripts 命名约定规定我们应该使用lowerCamelCase 或UPPER_CASE 作为const 变量的名称。
我怎样才能同时满足(React 和 tslint)?
我正在使用 Intellij 进行角度项目。我在编写代码时遇到了很多 tslint 错误。有没有办法通过在 intellij 中使用命令来修复所有这些 lint 错误?
我有一个抽象的基本组件,可以自己清理订阅:
import { OnDestroy, OnInit } from '@angular/core';
import { Subject } from 'rxjs/Subject';
export abstract class NeatComponent implements OnDestroy, OnInit {
// Add '.takeUntil(this.ngUnsubscribe)' before every '.subscrybe(...)'
// and this subscriptions will be cleaned up on component destroy.
protected ngUnsubscribe: Subject<any> = new Subject();
public ngOnDestroy() {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
}
public ngOnInit(){}
}Run Code Online (Sandbox Code Playgroud)
现在我创建工作组件:
export class CategorySelectorComponent extends NeatComponent {
public constructor() { super(); }
public ngOnInit() {
// some code
}
}Run Code Online (Sandbox Code Playgroud)
一切正常,但 tsLint 不喜欢我的 ngOnInit 方法:
[tslint] …
当我当前运行ng build命令时,我会遇到一大堆运行时问题,而这些问题在我以 JIT 模式(Angular 6)编译时不存在。
有没有办法在编译时获得所有 AOT 问题的列表,而无需手动找出它们?
我认为至少有一套tslint规则可以强制开发人员编写符合 AOT 的代码,但我找不到。我错过了什么吗?
任何帮助表示赞赏。
更新:我正在谈论的运行时问题之一是mat-icon来自 Angular Material 的 s 不起作用。它们只是呈现为文本,如“ chevron_left ”。显然,捆绑包不包含MatIconModule. 但是,JIT 一切都按预期工作。
此外,当我阅读 AOT 文档时,我看到 AOT 编译器不支持对本地(未导出)符号的引用。但是,我确实在我的装饰器中引用了本地符号,但是我没有收到任何编译错误通知我这一点。
我为我的 Angular 应用程序定义了一组 tslint 规则。但是,对于规范文件,我想使用更宽松的规则集(例如,newline-per-chained-call为好的期望语句禁用规则),但我无法弄清楚如何在 IntelliJ IDEA 中做到这一点。
Angular CLI 工作正常,因为在 CLI 配置中有一个部分将正确的 tsconfig.json 文件(具有正确的排除/包含)与相应的 tslint 文件映射。但是,IntelliJ 并不尊重这一点。
"lint": [
{
"project": "src/tsconfig.app.json"
},
{
"project": "src/tsconfig.spec.json"
"tslintConfig": "tslint.spec.json"
},
{
"project": "e2e/tsconfig.e2e.json",
"tslintConfig": "tslint.spec.json"
}
]
Run Code Online (Sandbox Code Playgroud)
IntelliJ 从 2018.1 版本开始支持多个 tsconfig.json 文件(参见:https ://youtrack.jetbrains.com/issue/WEB-28091 ),尽管看起来仍然不支持多个 tslint 配置。
我还找到了一个解决方案来创建一个本地范围并对其禁用一些检查,但我希望它由不使用 IntelliJ 的人的 tslint 规则定义。
另一种方法是将测试放在一个单独的目录中,并在其中放置一个 tslint.json,但这不是 Angular 推荐的,也不是人们在 Angular 中习惯的。
我最近在使用 TS 时遇到了这个问题,其中可能会意外访问全局变量(例如窗口属性)。
考虑这个例子:
a.ts
// a.ts
export const postMessage = (message: string) => console.log(message);
Run Code Online (Sandbox Code Playgroud)
b.ts
// Forget to import, but use libs: dom... so it compliles perfectly
// import { postMessage } from './a';
postMessage('hello'); // Oooops
Run Code Online (Sandbox Code Playgroud)
看来,tslint 不能也无意处理这个案子。说,TS 处理它。但可能是 TS 不...
IMO,所需的行为是禁止隐式访问全局变量(全局/窗口属性)
postMessage() // Error
window.postMessage() // OK
Run Code Online (Sandbox Code Playgroud)
它认为这就是 eslint no-undef规则所做的。
TS repo 中似乎存在一个未解决的问题,但它已经过时了(Microsoft/TypeScript#14306)
如果您知道任何解决此问题的食谱/信息,请分享。或者也许我忘记了一些东西并且很容易修复(因此,愚蠢的问题)
如何从被转译中排除打字稿文件,但仍确保它们与 Atom 编辑器中的 linter 一起正常工作?
我在我的*.spec.ts文件中收到此错误:
ES5/ES3 中的异步函数或方法需要“Promise”构造函数。确保您有“Promise”构造函数的声明或在您的
--lib选项中包含“ES2015” 。
出现问题是因为我明确排除了包含所有测试文件的目录(请参阅下面的 tsconfig 文件),因为我不希望在构建项目时将这些文件转换为 JavaScript。但是,当我在 Atom 编辑器中查看它们时,我确实希望 tslint 插件正确地对这些文件进行检查。
我的设置:
我的tsconfig.json文件:
{
"compileOnSave": false,
"compilerOptions": {
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"baseUrl": ".",
"declaration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"inlineSourceMap": true,
"inlineSources": true,
"lib": [
"es2017",
"dom"
],
"moduleResolution": "node",
"newLine": "lf",
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "./dist",
"target": "es5", …Run Code Online (Sandbox Code Playgroud) VS Code 无法识别开箱即用的 ES7 绑定语法。相反,它显示为语法错误:
[ts] Declaration or statement expected.
Run Code Online (Sandbox Code Playgroud)
.eslintrc,这似乎也不起作用。到目前为止,我什至无法确认 VS Code 识别我的.eslintrc文件并将其用于自己的配置。有没有人知道如何配置 VS Code 来识别语法?我意识到这是一个Stage 0功能,但是我觉得在这一点上它相当普遍。我也意识到我看到的是 TypeScript 错误,但是禁用 TypeScript linting 并不能消除错误。我认为它只是显示为继承自 JavaScript 的 TypeScript 错误。
经过大量搜索,有很多关于如何使用它的文章,但关于VS Code几乎没有。
tslint ×10
typescript ×7
angular ×4
angular-cli ×1
angular2-aot ×1
atom-editor ×1
eslint ×1
intellij-14 ×1
javascript ×1
lint ×1
reactjs ×1
tsconfig ×1