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

help: withDefaults() Typescript script setup #568

Open
Blemming opened this issue Oct 12, 2021 · 3 comments
Open

help: withDefaults() Typescript script setup #568

Blemming opened this issue Oct 12, 2021 · 3 comments
Assignees

Comments

@Blemming
Copy link

📚 What are you trying to do? Please describe.
Want to declare defaults to my component props in Typescript with the script setup as indicated here

🔍 What have you tried?

<script setup lang="ts">
withDefaults(defineProps<{prop:string}>(),{prop:'my default'})
</script>

says withDefaults is not defined

ℹ️ Additional context
I am running a nuxt 2 project with:

  • nuxt-vite
  • unplugin-auto-import
  • unplugin-vue2-script-setup
  • unplugin-vue-components

Anyone know how to do this ?

@yassilah
Copy link

yassilah commented Nov 5, 2021

Hi! I've had the same issue, it seems if props are not assigned to a variable, withDefaults doesn't work. The workaround is to assign it. This should work:

const _props = withDefaults(defineProps<{ prop: string }>(), { prop: 'my default' })

@fellz
Copy link

fellz commented Nov 8, 2021

It doesn't help.
It seems that nuxt 2+ does not recognize new macros defineProps and withDefaults. Am i right ? @danielroe

@gisu
Copy link

gisu commented Nov 10, 2021

I followed the example https://v3.vuejs.org/api/sfc-script-setup.html#typescript-only-features.

In Dev mode it works, but I have a problem with Typesscript. In the package the types for defineProps and withDefaults are not present. This will show up in the final build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants