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

Vue 3 Type 'string' is not assignable to type 'Ref<string>'

I have this code which works perfectly fine on my localhost. But Typescript throws me an error Type ‘string’ is not assignable to type Ref<string>.

let placeholderTitle = ref<string>('')


function beforeDragStart() {
    placeholderTitle = ''
    ....
}

Can somedy tell me please what is the problem with this code.

Secondly why this composition api allowes me to asign value straight to the placeholderTitle and placeholdertitle.value = throws me an error?

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

>Solution :

Being a Ref, placeholderTitle has to be a constant (const instead of let).

Using let allows you to change the content of placeholderTitle, but then Typescript throws legitimately a type error Type 'string' is not assignable to type Ref<string>.

To change the content of a Ref, you have to change its inner .value:

function beforeDragStart() {
    placeholderTitle.value = ''
    ....
}
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