diff --git a/packages/runtime-core/__tests__/componentProps.spec.ts b/packages/runtime-core/__tests__/componentProps.spec.ts index 26549cb1eff..c72319d683d 100644 --- a/packages/runtime-core/__tests__/componentProps.spec.ts +++ b/packages/runtime-core/__tests__/componentProps.spec.ts @@ -157,6 +157,7 @@ describe('component props', () => { test('default value', () => { let proxy: any const defaultFn = jest.fn(() => ({ a: 1 })) + const defaultBaz = jest.fn(() => ({ b: 1 })) const Comp = { props: { @@ -165,6 +166,10 @@ describe('component props', () => { }, bar: { default: defaultFn + }, + baz: { + type: Function, + default: defaultBaz } }, render() { @@ -177,7 +182,9 @@ describe('component props', () => { expect(proxy.foo).toBe(2) const prevBar = proxy.bar expect(proxy.bar).toEqual({ a: 1 }) + expect(proxy.baz).toEqual(defaultBaz) expect(defaultFn).toHaveBeenCalledTimes(1) + expect(defaultBaz).toHaveBeenCalledTimes(0) // #999: updates should not cause default factory of unchanged prop to be // called again diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index d1989b5c65b..5cd170f5f29 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -266,13 +266,16 @@ function resolvePropValue( key: string, value: unknown ) { - const opt = options[key] + const opt = options[key] as any if (opt != null) { const hasDefault = hasOwn(opt, 'default') // default values if (hasDefault && value === undefined) { const defaultValue = opt.default - value = isFunction(defaultValue) ? defaultValue() : defaultValue + value = + opt.type !== Function && isFunction(defaultValue) + ? defaultValue() + : defaultValue } // boolean casting if (opt[BooleanFlags.shouldCast]) {