Skip to content

Commit 1c45eae

Browse files
committed
fix(props): Warn type when not a constructor
1 parent 38e967b commit 1c45eae

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.filter(t => t).length;
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
@@ -543,4 +543,20 @@ describe('Options props', () => {
543543
expect(vm.$refs.test.$props.booleanOrString).toBe(true)
544544
expect(vm.$refs.test.$props.stringOrBoolean).toBe('')
545545
})
546+
547+
it('should warn when a prop type is not a constructor', () => {
548+
const vm = new Vue({
549+
template: '<div>{{a}}</div>',
550+
props: {
551+
a: {
552+
type: 'String',
553+
default: 'test'
554+
}
555+
}
556+
}).$mount()
557+
expect(
558+
'Invalid prop type: "String" is not a constructor'
559+
).toHaveBeenWarned()
560+
})
561+
546562
})

0 commit comments

Comments
 (0)