Skip to content

Commit

Permalink
dx(runtime-core): warn when the prop type is [] (vuejs#7608)
Browse files Browse the repository at this point in the history
  • Loading branch information
skirtles-code authored Nov 10, 2023
1 parent 58e2a94 commit a5491e1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/runtime-core/__tests__/componentProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ describe('component props', () => {
obj: { type: Object },
cls: { type: MyClass },
fn: { type: Function },
skipCheck: { type: [Boolean, Function], skipCheck: true }
skipCheck: { type: [Boolean, Function], skipCheck: true },
empty: { type: [] }
},
setup() {
return () => null
Expand All @@ -351,7 +352,8 @@ describe('component props', () => {
obj: 'false',
cls: {},
fn: true,
skipCheck: 'foo'
skipCheck: 'foo',
empty: [1, 2, 3]
}),
nodeOps.createElement('div')
)
Expand Down Expand Up @@ -379,6 +381,9 @@ describe('component props', () => {
expect(
`Invalid prop: type check failed for prop "skipCheck". Expected Boolean | Function, got String with value "foo".`
).not.toHaveBeenWarned()
expect(
`Prop type [] for prop "empty" won't match anything. Did you mean to use type Array instead?`
).toHaveBeenWarned()
})

// #3495
Expand Down
6 changes: 6 additions & 0 deletions packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,12 @@ function getInvalidTypeMessage(
value: unknown,
expectedTypes: string[]
): string {
if (expectedTypes.length === 0) {
return (
`Prop type [] for prop "${name}" won't match anything.` +
` Did you mean to use type Array instead?`
)
}
let message =
`Invalid prop: type check failed for prop "${name}".` +
` Expected ${expectedTypes.map(capitalize).join(' | ')}`
Expand Down

0 comments on commit a5491e1

Please sign in to comment.