Skip to content

Commit 86ceef4

Browse files
authored
fix(runtime-core): fix warning for absent props (#3363)
fix #3362
1 parent e4a5712 commit 86ceef4

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,23 @@ describe('component props', () => {
301301
}).not.toThrow(TypeError)
302302
})
303303

304+
test('warn absent required props', () => {
305+
const Comp = {
306+
props: {
307+
bool: { type: Boolean, required: true },
308+
str: { type: String, required: true },
309+
num: { type: Number, required: true }
310+
},
311+
setup() {
312+
return () => null
313+
}
314+
}
315+
render(h(Comp), nodeOps.createElement('div'))
316+
expect(`Missing required prop: "bool"`).toHaveBeenWarned()
317+
expect(`Missing required prop: "str"`).toHaveBeenWarned()
318+
expect(`Missing required prop: "num"`).toHaveBeenWarned()
319+
})
320+
304321
test('merging props from mixins and extends', () => {
305322
let setupProps: any
306323
let renderProxy: any

packages/runtime-core/src/componentProps.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export function initProps(
142142
setFullProps(instance, rawProps, props, attrs)
143143
// validation
144144
if (__DEV__) {
145-
validateProps(props, instance)
145+
validateProps(rawProps || {}, props, instance)
146146
}
147147

148148
if (isStateful) {
@@ -264,8 +264,8 @@ export function updateProps(
264264
// trigger updates for $attrs in case it's used in component slots
265265
trigger(instance, TriggerOpTypes.SET, '$attrs')
266266

267-
if (__DEV__ && rawProps) {
268-
validateProps(props, instance)
267+
if (__DEV__) {
268+
validateProps(rawProps || {}, props, instance)
269269
}
270270
}
271271

@@ -462,13 +462,17 @@ function getTypeIndex(
462462
/**
463463
* dev only
464464
*/
465-
function validateProps(props: Data, instance: ComponentInternalInstance) {
466-
const rawValues = toRaw(props)
465+
function validateProps(
466+
rawProps: Data,
467+
props: Data,
468+
instance: ComponentInternalInstance
469+
) {
470+
const resolvedValues = toRaw(props)
467471
const options = instance.propsOptions[0]
468472
for (const key in options) {
469473
let opt = options[key]
470474
if (opt == null) continue
471-
validateProp(key, rawValues[key], opt, !hasOwn(rawValues, key))
475+
validateProp(key, resolvedValues[key], opt, !hasOwn(rawProps, key))
472476
}
473477
}
474478

0 commit comments

Comments
 (0)