我是 AngularJS 2 的新手,这是我第一次尝试用 TSlint 编译它。我做了Tour of Heroes教程,有一个像下面这样的部分,TSlint不想编译说object access via string literals is disallowed.
ngOnInit(): void {
this.route.params.forEach((params: Params) => {
let id = +params['id'];
this.projectService.getProject(id)
.then(project => this.project = project);
});
}
Run Code Online (Sandbox Code Playgroud)
我不确定我是否理解这个问题,如果要解决它,我会迷失方向。请问你能帮帮我吗?
我的其他代码
getProjects(): Promise<Project[]> {
return this.http.get(`${this.configuration.Server}projects${this.configuration.ApiUrl}`)
.toPromise()
.then(response => response.json())
.catch(this.handleError);
}
getProject(ident: number): Promise<Project> {
return this.getProjects()
.then(projects => projects.find(project => project.id === id));
}
Run Code Online (Sandbox Code Playgroud) 如何在此规则下为以下内容编码?
let someArray = [...];
for (let i = 0, n = someArray.length; i < n; i++) {
...
}
Run Code Online (Sandbox Code Playgroud) 在我的打字稿项目中,我使用:
const program = require('commander');
const figlet = require('figlet');
const AWS = require('aws-sdk');
Run Code Online (Sandbox Code Playgroud)
并且我想重构这些行以使其工作import,以遵守 tslint 的no-var-requires规则。然而,它应该工作的方式让我望而却步。
对于 figlet,我都尝试了:
import figlet from 'figlet';
import * as figlet from 'figlet';
Run Code Online (Sandbox Code Playgroud)
然后我得到:
bin/console.ts(1,20): error TS2307: Cannot find module 'figlet'.
Run Code Online (Sandbox Code Playgroud)
我该如何导入这些库?
使用angular2 app我已经为我的vs代码安装了tslint扩展,
该应用程序工作正常,但tslint显示要更正的行,
import { Injectable } from '@angular/core';
@Injectable()
export class UserProfileService{
constructor() {
}
getProfile() {
return this.profile;
}
profile: Object = {
profilePic: 'http://www.appresume.com/cv_templates/cv_2_1/conf/images/profile.jpg',
firstName: 'John',
lastName:'Doe',
detailUrl : ''
};
}
Run Code Online (Sandbox Code Playgroud)
什么是修复?
在我的 angular 组件上,我使用了来自 RxJs 的两种方法,debounceTime()以及distinctUntilChanged()
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
@Component({
selector: 'app-form4th',
templateUrl: './form4th.component.html',
})
export class Form4thComponent implements OnInit {
searchField: FormControl;
searches: string[] = [];
constructor() { }
ngOnInit() {
this.searchField = new FormControl();
this.searchField
.valueChanges
.debounceTime(400)
.distinctUntilChanged()
.subscribe(term => {
this.searches.push(term);
});
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序工作正常,在执行(构建)ie 时没有错误甚至没有警告消息ng serve,并且在浏览器上运行应用程序按预期工作,并且在浏览器控制台上也没有错误消息或警告。
但是,我的 vscode 上有一条奇怪的 TSLint 消息说:
[ts] Property …
我正在尝试在VS代码中配置TS Lint的最大行长(除其他设置外),但是无论我进行什么更改都不会“占用”。
因此,第一个奇怪的是,VS代码的最大行长的TS Lint错误表示我已经超过了140个字符的限制,但是在各种配置文件中,我发现它仅提及120个字符作为默认字符。
我已将其更改为200个字符,已禁用/启用了扩展名,但仍然收到140个字符的警告。有谁知道在哪里以及如何配置此设置?联机文档足够清晰,但我似乎没有tslint.json文件,在node_modules => tslint => lib => rules文件夹中,设置为120,对其进行更改没有任何区别。
我正在导入一个文件:
import { BodyTableHeaderExampleModule } from '../../components/example-table/example-table-header/example-table-header-xxxx/example-table-header-xxxxx.module';
Run Code Online (Sandbox Code Playgroud)
但是,tslint 抱怨该行超过 100 个字符。当我尝试通过执行以下操作来最小化行长时:
import { BodyTableHeaderExampleModule } from '../../components/example-table/example-table-header/' +
'example-table-header-xxxx/example-table-header-xxxxx.module';
Run Code Online (Sandbox Code Playgroud)
我收到以下 tslint 错误:
ERROR: 11:92 semicolon Missing semicolon
ERROR: 11:93 no-unused-expression unused expression, expected an assignment or function call
Run Code Online (Sandbox Code Playgroud)
任何关于在Exceeds maximum line length of 100使用长 Typescript 文件导入时解决问题的建议都将不胜感激。谢谢你。
我试过了:
import { Observable } from 'rxjs/Observable';
Run Code Online (Sandbox Code Playgroud)
这给了我错误:
node_modules/rxjs/Obserable没有导出成员'Observable'
我试过了:
import { Observable } from 'rxjs';
Run Code Online (Sandbox Code Playgroud)
这给了我TSLINT错误:
此导入已列入黑名单
我知道我可以通过删除tslint.json中的'rxjs' import-blacklist条目来解决这个问题,但我不想这样做.我确信它有充分的理由.我如何正确进行导入?谢谢
编辑:更改为大写O - 请参阅评论
我正在尝试定义一个包含对象的对象模型。该对象可以是两种不同类型的对象。
export interface EventParams {
evtType: string;
evtData: FaultOrGoalData| SwapData;
}
export interface FaultOrGoalData {
evtName: string;
player: string;
position: string;
}
export interface SwapData {
swapPlayer: string;
}
Run Code Online (Sandbox Code Playgroud)
我在这里的问题是ts lint告诉我,不可能访问封装对象中包含的数据。
例: params.evtData.evtName
因此,我的问题是:是否可以使用接口创建联合类型?
create-react-app在过去的几个月里,我一直在开发几个+ TypeScript 应用程序。在某些时候(我可能升级了 lib) tslint 开始this.setState({ [name]: value })在状态被赋予类型时抛出错误。它曾经让它滑动(除非我快疯了......不是不可能的)。
让 tslint 闭嘴的唯一方法是将状态类型更改为any,我不想这样做。如果我保存文件并yarn start获取更改,它运行良好......所以我可以忽略 tslint 在 vscode 中抛出的错误。
我明白为什么 tslint 会抛出错误。我想知道是否有规则可以设置为忽略此特定错误?
我看到这个的例子(第5行):
class MyComponent extends React.Component<IMyComponentProps, IMyComponentState> {
...
private handleOnChange = (event: any) => {
const { name, value } = event.target;
this.setState({ [name]: value }); <-- tslint no likey
}
Run Code Online (Sandbox Code Playgroud)
错误:
[ts] '{ [x: number]: any; 类型的参数 }' 不可分配给类型为 'IMyComponentState| 的参数 ((prevState: Readonly, pro...'. Type '{ [x: number]: any; }' 不可分配给 type …
tslint ×10
typescript ×8
angular ×4
rxjs ×2
gulp ×1
javascript ×1
reactjs ×1
rxjs6 ×1
union-types ×1