Skip to content

fix(runtime-core): handle validate prop check edge case #12240

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions packages/runtime-core/__tests__/componentProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ describe('component props', () => {
fn: { type: Function },
skipCheck: { type: [Boolean, Function], skipCheck: true },
empty: { type: [] },
foo: { type: Boolean },
},
setup() {
return () => null
Expand All @@ -429,6 +430,7 @@ describe('component props', () => {
fn: true,
skipCheck: 'foo',
empty: [1, 2, 3],
foo: Symbol(),
}),
nodeOps.createElement('div'),
)
Expand Down Expand Up @@ -459,6 +461,9 @@ describe('component props', () => {
expect(
`Prop type [] for prop "empty" won't match anything. Did you mean to use type Array instead?`,
).toHaveBeenWarned()
expect(
`[Vue warn]: Invalid prop: type check failed for prop "foo". Expected Boolean, got Symbol`,
).toHaveBeenWarned()
})

// #3495
Expand Down
4 changes: 4 additions & 0 deletions packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import {
isOn,
isReservedProp,
isString,
isSymbol,
makeMap,
stringifySymbol,
toRawType,
} from '@vue/shared'
import { warn } from './warning'
Expand Down Expand Up @@ -795,6 +797,8 @@ function styleValue(value: unknown, type: string): string {
return `"${value}"`
} else if (type === 'Number') {
return `${Number(value)}`
} else if (isSymbol(value)) {
return stringifySymbol(value)
Comment on lines +800 to +801
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this check needs to come first, otherwise it'll still fail when a Symbol is passed for a prop with type: String.

} else {
return `${value}`
}
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/toDisplayString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const replacer = (_key: string, val: unknown): any => {
return val
}

const stringifySymbol = (v: unknown, i: number | string = ''): any =>
export const stringifySymbol = (v: unknown, i: number | string = ''): any =>
// Symbol.description in es2019+ so we need to cast here to pass
// the lib: es2016 check
isSymbol(v) ? `Symbol(${(v as any).description ?? i})` : v