Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Type 'string' is not assignable to type 'ComputedRef<string>'

I’ve got this code in my Vue app: What I want to do is change the color property based on the props. I also use computed, because the color property could change outside the component.

export default defineComponent({
  name: 'mail-dropdown',
  props: {
    title: {
      type: String,
      default: '',
      // required: true
    },
    id: {
      type: String,
    },
    shade: {
      type: String,
      default: 'white',
    },
    grey: {
      type: Boolean,
      default: false,
      // required: true
    },
    items: { type: Object as PropType<ActionItem[]> },
  },
  setup(props, context) {
    const store = useCounterStore();
    let color = computed(() => props.shade);
    if (props.grey == true) {
      color = 'grey-5';
    };
    
    return { store, color };
  },
});

I got an error here:

if (props.grey == true) {
  color = 'grey-5';
};

The error is the following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

> let color: ComputedRef<string> 
Type 'string' is not assignable to type 'ComputedRef<string>'.

What is the solution to changing the color?

>Solution :

You missed to add .value :

  if (props.grey == true) {
    color.value = 'grey-5';
 };

but computed property should be writable like :

 let color = computed({
   get(){
     return props.shade
   },
  set(val){
  //emit the new val or mutate the store state
   }
  });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading