简体   繁体   中英

In vue3 setup , how to both use defineProps with ts and value default?(Typescript)

I want to use ts types and value default in "defineProps", but it don't work. How can I solve it?

code below

const props = defineProps<{
  type?: string
  color?: 'color-primary' | 'color-danger' | 'color-plain' | undefined
}>({
  type: {
    default: 'plain',
  },
  color: {
    default: 'color-plain',
  },
})

But it will raise error in ts.

在此处输入图像描述

You need to use withDeaults

type Props = {
  type?: string
  color?: 'color-primary' | 'color-danger' | 'color-plain' | undefined
}
const props = withDefaults(defineProps<Props>(), {
  type: 'plain',
  color: 'color-plain',
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM