Subscribe on changes!

当使用function作为props的默认值时,setup中prop类型错误

avatar
Jul 24th 2021

Version

3.1.5

Reproduction link

Steps to reproduce

export default defineComponent({
    props: {
        id: {
            type: String,
            default: function() {
                return ‘any’;
            },
        },
    },
    setup(prop, context) {
      // prop类型错误
    }
})

image image

What is expected?

正确的ts类型

What is actually happening?

setup中prop的类型被识别错误

avatar
Jul 24th 2021

as a workaround: use arrow function instead.

    props: {
        id: {
            type: String,
            default: ()=>'any',
        },
    },
avatar
Jul 24th 2021

as a workaround: use arrow function instead.

    props: {
        id: {
            type: String,
            default: ()=>'any',
        },
    },

太棒了,可以正常工作了。原来的写法我是从官方文档中看到的。 https://www.vue3js.cn/docs/zh/guide/component-props.html#prop-%E9%AA%8C%E8%AF%81

最好可以把官方文档中的示例的用法修正为 arrow function

avatar
Jul 24th 2021

@wellfrog16 default: function () {return ''} 应该也可以推断出正确的类型。需要优化。

avatar
Jul 24th 2021

That's not a valid repro. Use the chat or forum for questions