M
M
Maxim Zolotoy2021-02-14 14:05:52
typescript
Maxim Zolotoy, 2021-02-14 14:05:52

How can you simplify the work with the object?

In the example below, I can't access the properties of an object by substituting the key in square brackets components.value[key] = true; unless I create a separate variable for the object and set it to type [key: string]: boolean

const componentsObj: {
        [key: string]: boolean
      } = {
        'core-preloader': false,
        'core-button': false,
        'core-progress': false,
        'core-alert': false,
        'core-toggler': true,
      }
      
      const components = ref(componentsObj);
      
      function toggleComponent(key: string) {
        components.value[key] = true;
      }


Is it possible to do something so as not to create an extra variable for which you also need to invent a name (I don’t want to pollute the code by creating useless variables)
and do something like this? And what would work in TS

const components = ref( {
        'core-preloader': false,
        'core-button': false,
        'core-progress': false,
        'core-alert': false,
        'core-toggler': true,
      });
      
      function toggleComponent(key: string) {
        components.value[key] = true;
      }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question