nuxt-community / composition-api

Composition API hooks for Nuxt 2.
https://composition-api.nuxtjs.org
MIT License
706 stars 101 forks source link

help: withDefaults() Typescript script setup #568

Open Blemming opened 2 years ago

Blemming commented 2 years ago

📚 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:

Anyone know how to do this ?

yassilah commented 2 years ago

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 commented 2 years ago

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

gisu commented 2 years ago

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.