Lint错误消息:
src/app/detail/edit/edit.component.ts [111,5]:for(... in ...)语句必须用if语句过滤
代码片段(这是一个工作代码.它也可以在angular.io表单验证部分获得):
for (const field in this.formErrors) {
// clear previous error message (if any)
this.formErrors[field] = '';
const control = form.get(field);
if (control && control.dirty && !control.valid) {
const messages = this.validationMessages[field];
for (const key in control.errors) {
this.formErrors[field] += messages[key] + ' ';
}
}
}
Run Code Online (Sandbox Code Playgroud)
知道如何修复这个lint错误吗?
我正在研究ng2实现.我正在使用以下函数调用将对象转换为数组:
var authors = Object.entries(responseObject.Authors);
Run Code Online (Sandbox Code Playgroud)
这是标准的js功能.但是,ts编译器返回以下错误:
"Property 'entries' does not exist on type 'ObjectConstructor'"
Run Code Online (Sandbox Code Playgroud)
基于快速谷歌,似乎解决方案可能是将compilerOptions目标属性从es5更改为es6.但是,在之前的一个问题的一些先前的研究之后,我认为我能够通过在我的tsconfig.json上包含额外的"lib"属性来利用es6功能:
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"module": "commonjs",
"noEmitOnError": true,
"noImplicitAny": false,
"outDir": "../Scripts/",
"removeComments": false,
"sourceMap": true,
"target": "es5",
"moduleResolution": "node",
"lib": [
"es2015",
"dom"
]
}
Run Code Online (Sandbox Code Playgroud)
我也尝试将目标属性更改为es2015,然后重建项目并执行"typescriptUsingTsConfig",但我仍然得到相同的错误.知道我可以在这里做什么来利用Object.entries()函数?
我有使用TypeScript转换ES7代码的问题.这段代码:
const sizeByColor = {
red: 100,
green: 500,
};
for ( const [ color, size ] of Object.entries(sizeByColor) ) {
console.log(color);
console.log(size);
}
Run Code Online (Sandbox Code Playgroud)
给出错误:
TypeError: Object.entries is not a function
TypeScript v2.0.3
tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"noEmitOnError": true,
"outDir": "dist",
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"pretty": true,
"lib": [ "es2017" ],
},
"exclude": [
"node_modules"
],
"include": [
"./node_modules/@types/**/*.d.ts",
"./src/**/*.ts"
]
}
Run Code Online (Sandbox Code Playgroud)
我想通过对象迭代Object.entries(),所以我分配了内部定义"lib": [ "es2017" ],但仍然,typescript不允许我转换它.
value = {
5c03dbc3d8e3e3435f875a46: "1",
5c03dc0fd8e3e3435f875a48: "2",
5c03dc30d8e3e3435f875a49: "3",
5c03dc5fd8e3e3435f875a4a: "4"
}
Run Code Online (Sandbox Code Playgroud)
我有一个这样的数组。我想从这个数组中获取键,以便我可以使用它来获取与这些键关联的值。这一切都是为了打字稿。
typescript ×3
angular ×2
angular-cli ×1
angular7 ×1
ecmascript-6 ×1
es2017 ×1
javascript ×1
tslint ×1