From 5cbc923d1b867b809ac7198b6f893fdbb5812f90 Mon Sep 17 00:00:00 2001 From: James Nakagawa Date: Tue, 17 Dec 2024 21:21:19 +0900 Subject: [PATCH 1/2] fix(core): Add warning message if prop type=null and value=null Motivation: defineProps will generate type=null if the static type cannot be inferred into a basic type. However, this also disables required checking. Adds a warning when the prop's type=null and the value is also null. --- packages/runtime-core/__tests__/componentProps.spec.ts | 2 ++ packages/runtime-core/src/componentProps.ts | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/packages/runtime-core/__tests__/componentProps.spec.ts b/packages/runtime-core/__tests__/componentProps.spec.ts index b8eb0e47208..42124367ea2 100644 --- a/packages/runtime-core/__tests__/componentProps.spec.ts +++ b/packages/runtime-core/__tests__/componentProps.spec.ts @@ -389,6 +389,7 @@ describe('component props', () => { bool: { type: Boolean, required: true }, str: { type: String, required: true }, num: { type: Number, required: true }, + null: { type: null, required: true }, }, setup() { return () => null @@ -398,6 +399,7 @@ describe('component props', () => { expect(`Missing required prop: "bool"`).toHaveBeenWarned() expect(`Missing required prop: "str"`).toHaveBeenWarned() expect(`Missing required prop: "num"`).toHaveBeenWarned() + expect(`Missing required prop: "null"`).toHaveBeenWarned() }) test('warn on type mismatch', () => { diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index 8baa7808665..fce757cd41b 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -688,6 +688,11 @@ function validateProp( if (value == null && !required) { return } + // null type but required + if (type == null && required && value == null) { + warn('Missing required prop: "' + name + '"') + return + } // type check if (type != null && type !== true && !skipCheck) { let isValid = false From 5d65b1e270128258906efd4d44e651f2332057b7 Mon Sep 17 00:00:00 2001 From: James Nakagawa Date: Tue, 17 Dec 2024 21:32:43 +0900 Subject: [PATCH 2/2] fix(core): Add more tests to the previous commit Add more tests --- .../runtime-core/__tests__/componentProps.spec.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/runtime-core/__tests__/componentProps.spec.ts b/packages/runtime-core/__tests__/componentProps.spec.ts index 42124367ea2..1fc019ff2a4 100644 --- a/packages/runtime-core/__tests__/componentProps.spec.ts +++ b/packages/runtime-core/__tests__/componentProps.spec.ts @@ -389,17 +389,24 @@ describe('component props', () => { bool: { type: Boolean, required: true }, str: { type: String, required: true }, num: { type: Number, required: true }, - null: { type: null, required: true }, + null1: { type: null, required: true }, + null2: { type: null, required: true }, + null3: { type: null, required: true }, }, setup() { return () => null }, } - render(h(Comp), nodeOps.createElement('div')) + render( + h(Comp, { null2: null, null3: undefined }), + nodeOps.createElement('div'), + ) expect(`Missing required prop: "bool"`).toHaveBeenWarned() expect(`Missing required prop: "str"`).toHaveBeenWarned() expect(`Missing required prop: "num"`).toHaveBeenWarned() - expect(`Missing required prop: "null"`).toHaveBeenWarned() + expect(`Missing required prop: "null1"`).toHaveBeenWarned() + expect(`Missing required prop: "null2"`).toHaveBeenWarned() + expect(`Missing required prop: "null3"`).toHaveBeenWarned() }) test('warn on type mismatch', () => {