]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): fix null type in required + multi-type prop declarations
authorEvan You <yyx990803@gmail.com>
Mon, 19 Jul 2021 15:20:28 +0000 (11:20 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 19 Jul 2021 15:20:28 +0000 (11:20 -0400)
fix #4146 (in combination with #4147)

packages/runtime-core/__tests__/componentProps.spec.ts
packages/runtime-core/src/componentProps.ts

index ac1dc13dfcca6fb718dc7b03bbd0df45cbffef26..863b12d0699095d5d9ee0eb797d97afbda076390 100644 (file)
@@ -556,4 +556,21 @@ describe('component props', () => {
     await nextTick()
     expect(serializeInner(root)).toBe(`foo`)
   })
+
+  test('support null in required + multiple-type declarations', () => {
+    const Comp = {
+      props: {
+        foo: { type: [Function, null], required: true }
+      },
+      render() {}
+    }
+    const root = nodeOps.createElement('div')
+    expect(() => {
+      render(h(Comp, { foo: () => {} }), root)
+    }).not.toThrow()
+
+    expect(() => {
+      render(h(Comp, { foo: null }), root)
+    }).not.toThrow()
+  })
 })
index c4ab05dd021be0ef6bed514480356f42b2be21ea..6cc6746a57e832a458c509e45ff3d031c285f0c3 100644 (file)
@@ -529,7 +529,7 @@ function validatePropName(key: string) {
 // so that it works across vms / iframes.
 function getType(ctor: Prop<any>): string {
   const match = ctor && ctor.toString().match(/^\s*function (\w+)/)
-  return match ? match[1] : ''
+  return match ? match[1] : ctor === null ? 'null' : ''
 }
 
 function isSameType(a: Prop<any>, b: Prop<any>): boolean {
@@ -637,6 +637,8 @@ function assertType(value: unknown, type: PropConstructor): AssertionResult {
     valid = isObject(value)
   } else if (expectedType === 'Array') {
     valid = isArray(value)
+  } else if (expectedType === 'null') {
+    valid = value === null
   } else {
     valid = value instanceof type
   }
@@ -656,7 +658,7 @@ function getInvalidTypeMessage(
 ): string {
   let message =
     `Invalid prop: type check failed for prop "${name}".` +
-    ` Expected ${expectedTypes.map(capitalize).join(', ')}`
+    ` Expected ${expectedTypes.map(capitalize).join(' | ')}`
   const expectedType = expectedTypes[0]
   const receivedType = toRawType(value)
   const expectedValue = styleValue(value, expectedType)