Skip to content

Commit

Permalink
fix(props): Warn type when not a constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Zarad1993 committed Dec 22, 2018
1 parent 38e967b commit 1c45eae
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/core/util/props.js
Expand Up @@ -122,13 +122,14 @@ function assertProp (
type = [type]
}
for (let i = 0; i < type.length && !valid; i++) {
const assertedType = assertType(value, type[i])
const assertedType = assertType(value, type[i], vm)
expectedTypes.push(assertedType.expectedType || '')
valid = assertedType.valid
}
}

if (!valid) {
const haveExpectedTypes = expectedTypes.filter(t => t).length;
if (!valid && haveExpectedTypes) {
warn(
getInvalidTypeMessage(name, value, expectedTypes),
vm
Expand All @@ -148,7 +149,7 @@ function assertProp (

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

function assertType (value: any, type: Function): {
function assertType (value: any, type: Function, vm: ?Component): {
valid: boolean;
expectedType: string;
} {
Expand All @@ -166,7 +167,12 @@ function assertType (value: any, type: Function): {
} else if (expectedType === 'Array') {
valid = Array.isArray(value)
} else {
valid = value instanceof type
try {
valid = value instanceof type
} catch (e) {
warn('Invalid prop type: "' + String(type) + '" is not a constructor', vm);
valid = false;
}
}
return {
valid,
Expand Down
16 changes: 16 additions & 0 deletions test/unit/features/options/props.spec.js
Expand Up @@ -543,4 +543,20 @@ describe('Options props', () => {
expect(vm.$refs.test.$props.booleanOrString).toBe(true)
expect(vm.$refs.test.$props.stringOrBoolean).toBe('')
})

it('should warn when a prop type is not a constructor', () => {
const vm = new Vue({
template: '<div>{{a}}</div>',
props: {
a: {
type: 'String',
default: 'test'
}
}
}).$mount()
expect(
'Invalid prop type: "String" is not a constructor'
).toHaveBeenWarned()
})

})

0 comments on commit 1c45eae

Please sign in to comment.