简体   繁体   English

将变量声明为 object 值时,TSlint 错误“object 文字中的预期属性简写......”

[英]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.

相关问题 对象文字属性值速记 - Object Literal Property Value Shorthand 对象文字属性值速记与`this`不兼容 - Object Literal Property Value Shorthand incompatible with `this` Python 是否支持 object 文字属性值简写,如 ECMAScript 6? - Does Python support object literal property value shorthand, a la ECMAScript 6? 声明对象文字时出现意外的标识符错误 - unexpected identifier error when declaring object literal ReactJS:预期属性速记对象速记 - ReactJS: Expected property shorthand object-shorthand 在对象文字中设置为变量时,Javascript速记三元运算符不起作用 - Javascript shorthand ternary operator not working when setting to variable in object literal Mongodb - 你能用 javascript 对象文字属性值速记更新两个值吗? - Mongodb - Can you update two values, with a javascript Object Literal Property Value Shorthand? 在对象属性速记中使用`get`时出现语法错误 - Syntax error when using `get` in object property shorthand 在将变量声明为函数Object的新实例时,为什么IE6会出现“期望函数”错误? - Why does IE6 give a “Function expected” error when declaring a variable as an new instance of a function Object? 具有可变属性的内联对象文字 - Inline object literal with a variable property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM