[英]TSlint error “Expected property shorthand in object literal…” when declaring a variable to a object value
I have an object like this:我有一个像这样的 object:
{
element: 'tool-app',
file: '/tool-app.js',
icon: 'csr-icon',
name: 'Planning view',
id: 'planning-view'
}
I want to store the value of icon in a variable and use it instead.我想将 icon 的值存储在一个变量中并使用它。 First I define a constant:
首先我定义一个常量:
const icon = 'csr-icon';
I then try to use the above icon to the object:然后我尝试对 object 使用上面的图标:
{
element: 'tool-app',
file: '/tool-app.js',
icon: icon, ///change here
name: 'Planning view',
id: 'planning-view'
}
It should be equal, but Tslint returns an error:它应该是相等的,但是 Tslint 返回一个错误:
Expected property shorthand in object literal ('{icon}'). (object-literal-shorthand)tslint(1)
How come?怎么来的?
This error tells you that instead of icon: icon,
, you can write only icon
because the property name is the same as your const.这个错误告诉你,你可以只写
icon
而不是icon: icon,
,因为属性名称与你的 const 相同。
So your new object should look like this:所以你的新 object 应该是这样的:
{
element: 'tool-app',
file: '/tool-app.js',
icon,
name: 'Planning view',
id: 'planning-view'
}
This is called "shorthand":)这被称为“速记”:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.