Skip to content

Commit 6940131

Browse files
Zarad1993sid-maddyposva
authoredMar 30, 2021
fix(warn): better message with no constructors props (#9241)
Co-authored-by: Siddhesh Mhadnak <[email protected]> Co-authored-by: Eduardo San Martin Morote <[email protected]>
1 parent 7c75462 commit 6940131

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed
 

‎src/core/util/props.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,14 @@ function assertProp (
122122
type = [type]
123123
}
124124
for (let i = 0; i < type.length && !valid; i++) {
125-
const assertedType = assertType(value, type[i])
125+
const assertedType = assertType(value, type[i], vm)
126126
expectedTypes.push(assertedType.expectedType || '')
127127
valid = assertedType.valid
128128
}
129129
}
130130

131-
if (!valid) {
131+
const haveExpectedTypes = expectedTypes.some(t => t)
132+
if (!valid && haveExpectedTypes) {
132133
warn(
133134
getInvalidTypeMessage(name, value, expectedTypes),
134135
vm
@@ -148,7 +149,7 @@ function assertProp (
148149

149150
const simpleCheckRE = /^(String|Number|Boolean|Function|Symbol)$/
150151

151-
function assertType (value: any, type: Function): {
152+
function assertType (value: any, type: Function, vm: ?Component): {
152153
valid: boolean;
153154
expectedType: string;
154155
} {
@@ -166,7 +167,12 @@ function assertType (value: any, type: Function): {
166167
} else if (expectedType === 'Array') {
167168
valid = Array.isArray(value)
168169
} else {
169-
valid = value instanceof type
170+
try {
171+
valid = value instanceof type
172+
} catch (e) {
173+
warn('Invalid prop type: "' + String(type) + '" is not a constructor', vm);
174+
valid = false;
175+
}
170176
}
171177
return {
172178
valid,

‎test/unit/features/options/props.spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -553,4 +553,20 @@ describe('Options props', () => {
553553
expect(vm.$refs.test.$props.booleanOrString).toBe(true)
554554
expect(vm.$refs.test.$props.stringOrBoolean).toBe('')
555555
})
556+
557+
it('should warn when a prop type is not a constructor', () => {
558+
const vm = new Vue({
559+
template: '<div>{{a}}</div>',
560+
props: {
561+
a: {
562+
type: 'String',
563+
default: 'test'
564+
}
565+
}
566+
}).$mount()
567+
expect(
568+
'Invalid prop type: "String" is not a constructor'
569+
).toHaveBeenWarned()
570+
})
571+
556572
})

0 commit comments

Comments
 (0)