Props should be immutable as default
Vue version
3.3.4
Link to minimal reproduction
Steps to reproduce
In the pinia I created readonly ref
I send props in the component
and in the component I get props
All is working, but type-check
send:
error TS4104: The type 'readonly ({ readonly id: string;} | { ...; } | { ...; })[]' is 'readonly' and cannot be assigned to the mutable type 'Item[]'.
What is expected?
props shouldn't be mutable
https://vuejs.org/guide/components/props.html#one-way-data-flow
What is actually happening?
props are mutable
System Info
System:
OS: macOS 14.0
CPU: (10) arm64 Apple M1 Pro
Memory: 87.11 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.3.0 - /opt/homebrew/bin/node
Yarn: 1.22.19 - /opt/homebrew/bin/yarn
npm: 9.6.7 - /opt/homebrew/bin/npm
Browsers:
Chrome: 114.0.5735.198
Safari: 17.0
npmPackages:
vue: ^3.3.4 => 3.3.4
Any additional comments?
I can wrap Readonly
P.s. And I catch errors in some libs https://github.com/anish2690/vue-draggable-next/issues/56 who is using it
- const readOnlyMessage = readonly(msg);
+ const readOnlyMessage = msg.value;
import { toRaw } from 'vue';
structuredClone(toRaw(items));
<script type="ts" setup>
import { reactive, readonly, watchEffect } from 'vue';
const original = reactive({ count: 0 });
const copy = readonly(original);
watchEffect(() => {
// works for reactivity tracking
console.log(copy.count);
});
// mutating original will trigger watchers relying on the copy
original.count += 1;
// mutating the copy will fail and result in a warning
// copy.count += 1; // warning!
</script>
<template>
<pre>{{ original }}</pre>
<pre>{{ copy }}</pre>
</template>