Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v3.5.x] destructure from defineProps with withDefaults throw an error #11930

Closed
zhaojjiang opened this issue Sep 15, 2024 · 4 comments
Closed

Comments

@zhaojjiang
Copy link

Vue version

3.5.5

Link to minimal reproduction

https://play.vuejs.org/#eNp9kctOwzAQRX9l8CatVFohWFVpEY9KwAIqQGLjTZROUhfHtuxJWynKvzNx1McCdeXM3DvxuZ5GPDg33tYopiINuVeOICDVDnRmypkUFKSYS6MqZz1BAx4LaKHwtoKExxJppMmtCQRVKGHW6YPkBbW28GO9Xl0lw5OlAUI+WvbtFK2fschqTWGwwkIZXHrrQtpIA9F2P4VAXplSmnY+GI7gqEwh6Q6+u+Wfp5Oemym5IKyczgi5AkjXN/OmiWRtm064il1lXE2wva7sCjVnZF0KmLCYTs7mxYjTM3ihyvEmWMNPFBGkyG3llEb/4UhxMCmmPVynZRx99xZ75GscHfr5GvPff/qbsO96Uiw9BvRblOKoUeZLpF5efL3jnr+PItPXmt0XxE8MVtcdY297rM2Ksc98kfY1Lpdf+jss9oQmHEJ1oJ2zjX4peOFPF6KfcG/Hd3GONyTaP1bly/I=

Steps to reproduce

create new project

// App.vue

<script setup lang="ts">
const { test } = withDefaults(defineProps<{
  test?: string
}>(), {
  test: 'test'
})
</script>

throw error:

error during build:
[vite:vue] [@vue/compiler-sfc] withDefaults() is unnecessary when using destructure with defineProps().
Prefer using destructure default values, e.g. const { foo = 1 } = defineProps(...).

D:/temp/vite/src/App.vue
1  |  <script setup lang="ts">
2  |  const { test } = withDefaults(defineProps<{
   |                   ^^^^^^^^^^^^
3  |    test?: string
4  |  }>(), {

What is expected?

It should not throw an error and shoule not break build, just keep it warning,

Or, if withDefaults not marked as deprecated, it should work without any warn or error messages

What is actually happening?

It breaks build

System Info

No response

Any additional comments?

No response

@Tofandel
Copy link
Contributor

Can I ask, why is it unnecessary when using destructure with defineProps? How do you pass defaults in this case then?

@Justineo
Copy link
Member

Can I ask, why is it unnecessary when using destructure with defineProps? How do you pass defaults in this case then?

const { foo = 1 } = defineProps()

@Tofandel
Copy link
Contributor

Tofandel commented Sep 27, 2024

But then they are still reactive right? If my understanding is correct there is some magic normally happening when using const { foo } = defineProps(); that makes foo reactive, or is it only something in nuxt?

I'm asking because now in the error message in question there is also Reactive destructure will be disabled when using withDefaults()

@zhaojjiang
Copy link
Author

It means defineProps + withDefaults works same as before, --> destrcuture is non-reactive,
and destructure with default value const { foo = 1 } will keep reactive

@github-actions github-actions bot locked and limited conversation to collaborators Oct 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants