Skip to content

Commit bbf6ca9

Browse files
committed
fix(runtime-core): fix null type in required + multi-type prop declarations
fix #4146 (in combination with #4147)
1 parent cac6ab5 commit bbf6ca9

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -556,4 +556,21 @@ describe('component props', () => {
556556
await nextTick()
557557
expect(serializeInner(root)).toBe(`foo`)
558558
})
559+
560+
test('support null in required + multiple-type declarations', () => {
561+
const Comp = {
562+
props: {
563+
foo: { type: [Function, null], required: true }
564+
},
565+
render() {}
566+
}
567+
const root = nodeOps.createElement('div')
568+
expect(() => {
569+
render(h(Comp, { foo: () => {} }), root)
570+
}).not.toThrow()
571+
572+
expect(() => {
573+
render(h(Comp, { foo: null }), root)
574+
}).not.toThrow()
575+
})
559576
})

packages/runtime-core/src/componentProps.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ function validatePropName(key: string) {
529529
// so that it works across vms / iframes.
530530
function getType(ctor: Prop<any>): string {
531531
const match = ctor && ctor.toString().match(/^\s*function (\w+)/)
532-
return match ? match[1] : ''
532+
return match ? match[1] : ctor === null ? 'null' : ''
533533
}
534534

535535
function isSameType(a: Prop<any>, b: Prop<any>): boolean {
@@ -637,6 +637,8 @@ function assertType(value: unknown, type: PropConstructor): AssertionResult {
637637
valid = isObject(value)
638638
} else if (expectedType === 'Array') {
639639
valid = isArray(value)
640+
} else if (expectedType === 'null') {
641+
valid = value === null
640642
} else {
641643
valid = value instanceof type
642644
}
@@ -656,7 +658,7 @@ function getInvalidTypeMessage(
656658
): string {
657659
let message =
658660
`Invalid prop: type check failed for prop "${name}".` +
659-
` Expected ${expectedTypes.map(capitalize).join(', ')}`
661+
` Expected ${expectedTypes.map(capitalize).join(' | ')}`
660662
const expectedType = expectedTypes[0]
661663
const receivedType = toRawType(value)
662664
const expectedValue = styleValue(value, expectedType)

0 commit comments

Comments
 (0)