最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TSlint error "Expected property shorthand in object literal..." when declaring a variable to a ob

programmeradmin2浏览0评论

I have an object like this:

   {
      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. First I define a constant:

const icon = 'csr-icon';

I then try to use the above icon to the 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:

Expected property shorthand in object literal ('{icon}'). (object-literal-shorthand)tslint(1)

How come?

I have an object like this:

   {
      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. First I define a constant:

const icon = 'csr-icon';

I then try to use the above icon to the 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:

Expected property shorthand in object literal ('{icon}'). (object-literal-shorthand)tslint(1)

How come?

Share Improve this question asked Mar 22, 2021 at 9:33 SharePointBeginnerSharePointBeginner 3591 gold badge3 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 22

This error tells you that instead of icon: icon,, you can write only icon because the property name is the same as your const.

So your new object should look like this :

{
  element: 'tool-app',
  file: '/tool-app.js',
  icon,
  name: 'Planning view',
  id: 'planning-view'
}

This is called "shorthand" :)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论