I'm currently in the process of converting some Vue3 code from javascript to typescript, and I am struggling to comprehend the unwrapping of a ref/computed value in the template.
I used to believe that the template would automatically unwrap reactive properties, but it appears that this is no longer the case.
Here is the code snippet:
<script setup lang="ts">
import { ref } from 'vue';
class BaseBlock {
style = ref('color: black')
}
interface IRefBlockKey {
[key: string]: BaseBlock;
}
let Block = new BaseBlock();
let refBlock = ref(new BaseBlock());
let refWithKeyBlock = ref({ myBlock: new BaseBlock() });
let refWithInterface = ref<IRefBlockKey>({ myBlock: new BaseBlock() });
</script>
<template>
<div :style="Block.style">Error in IDE</div>
<div :style="Block.style.value">No Error</div>
<div :style="refBlock.style">No Error</div>
<div :style="refWithKeyBlock['myBlock'].style">No Error (ok...?)</div>
<div :style="refWithInterface['myBlock'].style">Error in IDE (why now!!)</div>
</template>
The first and last div
are both causing an error in my IDE
Type 'Ref<string>' is not assignable to type 'StyleValue | undefined'.
.The fact that the first div isn't working has left me feeling disoriented, but the issue with the last one is really puzzling.
Can anyone provide guidance on understanding what exactly is happening here?