将我的 Angular 12 项目从 TSLint 迁移到 ESLint 后,为每个项目创建了一个 eslintrc 文件,每个文件都有一行将 parseroptions.projects 设置为如下所示的路径:
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"parserOptions": {
"project": ["apps/my-app/tsconfig.*?.json"]
},
...
],
...
}
Run Code Online (Sandbox Code Playgroud)
我一直在阅读 eslint 文档试图找到这个问题的答案,但没有找到任何有用的东西。
我们最近迁移到一个包含许多 Angular 11+ 应用程序和库的新项目。我们已经angularCompilerOptions.strictTemplates: true为所有应用程序设置了。
问题是我们有一个 CI 管道来检查格式并运行 eslint,但严格的模板检查错误在我们进行生产构建之前不会被标记。因此,我们必须在 CI 中构建所有受影响的应用程序,如果我们更改库组件,则需要检查和构建所有应用程序。这可能需要几个小时。
有没有一种方法可以让 eslint/tslint 检查任何严格的模板错误,而无需每次都构建应用程序?
这是我们的 eslint.json:
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts"],
"extends": ["plugin:@nrwl/nx/angular", "plugin:@angular-eslint/template/process-inline-templates"],
"parserOptions": { "project": ["apps/to-home/tsconfig.*?.json"] },
"rules": {
"@angular-eslint/directive-selector": ["error", { "type": "attribute", "prefix": "toh", "style": "camelCase" }],
"@angular-eslint/component-selector": ["error", { "type": "element", "prefix": "toh", "style": "kebab-case" }]
}
},
{ "files": ["*.html"], "extends": ["plugin:@nrwl/nx/angular-template"], "rules": {} }
]
}
Run Code Online (Sandbox Code Playgroud)
和根 json:
{
"root": true,
"ignorePatterns": ["**/*"],
"plugins": ["@nrwl/nx"],
"overrides": …Run Code Online (Sandbox Code Playgroud) 在我的代码上运行tslint我收到此错误:
expected variableDeclarator: 'getCode' to have a typedef.
Run Code Online (Sandbox Code Playgroud)
对于TypeScript文件:
export var getCode = function (param: string): string {
// ...
};
Run Code Online (Sandbox Code Playgroud)
我如何改进这一点,所以我没有看到tslint错误?
我正在尝试在我的LoginService中实现isLoggedIn类型的布尔值BehaviorSubject以及getter和setter函数,以获取值作为Observable /通过其BehaviorSubject正确设置变量.这是有效的,但它在TSLint中引发了两个关于"Type not assignable"和"Dublicate identifier"的错误.在没有TSLint抱怨的情况下,定义它的正确方法是什么.
这是上述代码的精简版:
@Injectable()
export class LoginService {
public isLoggedInSource = new BehaviorSubject<boolean>(false);
public isLoggedIn: Observable<boolean> = this.isLoggedInSource.asObservable(); // Duplicate identifier 'isLoggedIn'.
constructor(private http: Http) {}
set isLoggedIn(logged): void { // Duplicate identifier 'isLoggedIn'.
this.isLoggedInSource.next(logged);
}
get isLoggedIn(): Observable<boolean> { // Duplicate identifier 'isLoggedIn'.
return this.isLoggedInSource.asObservable();
}
logout() {
this.isLoggedIn = false; // Type 'boolean' is not assignable to type 'Observable<boolean>'.
}
login(body) {
return this.http.post('/login', body)
.map(res => {
if (res.token) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试在TypeScript中创建格式正确的SVG元素:
createSVGElement(tag) {
return document.createElementNS("http://www.w3.org/2000/svg", tag);
}
Run Code Online (Sandbox Code Playgroud)
但是,我在以下错误 tslint
字符串中禁止的http网址:“ http://www.w3.org/2000/svg ”
如何避免此错误?我以为我需要此URL才能满足SVG标准?
我正在使用以下typescript方法来生成UUIDs.代码本身基本上是这个stackoverflow答案的打字稿版本.
generateUUID(): string {
let date = new Date().getTime();
if (window.performance && typeof window.performance.now === 'function') {
date += performance.now();
}
let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
let r = (date + Math.random() * 16) % 16 | 0;
date = Math.floor(date / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
return uuid;
};
Run Code Online (Sandbox Code Playgroud)
我们的开发团队TSLint用来保持代码清洁,我们有一个禁止使用的规则bitwise operators.我不知道如何在不损害UUID生成器的加密方面的情况下重写此代码.如何重写这段代码或者这根本没有意义呢?
阅读最新的Firebase博客文章为什么你应该使用TypeScript编写云函数,我决定尝试tslint它,这很神奇,虽然我的类型有问题.
我有一个import语句
import { DocumentSnapshot, DocumentReference, QuerySnapshot, WriteResult, Transaction, WriteBatch } from '@google-cloud/firestore';
Run Code Online (Sandbox Code Playgroud)
但即使我的代码工作正常,tslint告诉我以下内容.
[tslint]模块'@ google-cloud/firestore'未在package.json中列为依赖项(no-implicit-dependencies)
Firebase + TypeScript使用/导入类型的最佳做法是什么?
我是打字新手。我已tslint.json创建文件,但对如何使用命令行运行此文件一无所知。请告知我需要使用哪个命令来运行此配置
简单的事情将导致这种情况发生。
let _tick = 0;
this.app.ticker.add( () => {
moveSprites(dots);
_tick += .2;
return;
});
Run Code Online (Sandbox Code Playgroud)
Tslint选项设置为以下内容:
"rules": {
"object-literal-sort-keys": false,
"no-unused-variable": [true, {"ignore-pattern": "^_"}]
}
Run Code Online (Sandbox Code Playgroud)
通过搜索,我认为该规则将解决并允许其被忽略但不能。
一种解决方案是这样编写。它会通过,但是会抱怨滴答+ = .2被分配但从未使用过。此外,这会改变行为。
this.app.ticker.add( (tick = 0) => {
moveSprites(dots);
tick += .2;
return;
});
Run Code Online (Sandbox Code Playgroud)
终于,我发现// @ ts-ignore并成功了……我是打字稿新手,我可以看到实例中的问题是,您只需要保持变量状态即可;仅设置它。我还将_var名称的一些约定视为受保护的类字段,但对于这些实例也是如此?正确的方法是什么?我喜欢TS的好处,但是作为一个新手,我会花费很多时间使ts linter变得愉快。