Skip to content

fix(warn): better message with no constructors props #9241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/core/util/props.js
Original file line number Diff line number Diff line change
@@ -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.some(t => t)
if (!valid && haveExpectedTypes) {
warn(
getInvalidTypeMessage(name, value, expectedTypes),
vm
@@ -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;
} {
@@ -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,
16 changes: 16 additions & 0 deletions test/unit/features/options/props.spec.js
Original file line number Diff line number Diff line change
@@ -553,4 +553,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()
})

})