Skip to content

Empty type name in warning when expected prop type is a custom class #7197

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

Closed
dotSILENT opened this issue Nov 23, 2022 · 4 comments · Fixed by #7198
Closed

Empty type name in warning when expected prop type is a custom class #7197

dotSILENT opened this issue Nov 23, 2022 · 4 comments · Fixed by #7198
Labels
🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed.

Comments

@dotSILENT
Copy link

dotSILENT commented Nov 23, 2022

Vue version

3.2.45

Link to minimal reproduction

https://sfc.vuejs.org/#__DEV____SSR__eNp9UstO6zAQ/ZW53rRI1Na9FzZRi0CwYIPEoktvQjJtA/FDHqcFRfl3Jm5SoEA3iWbO8Zwzj1bceC+3DYpMzKkIlY9AGBt/pW1lvAsRWgi4gg5WwRmYMHVygB7ebuucaICkGmL5TB+cW2f8gdAHvRrD2hbOUoTKbvO6Kh+D88s3j7AAizu4yyNOz0bOT4xBazpZbiqCtUOCHdb1hB/N1b4T7oGDiMbXXI4jgPnmb/oD3DPZwc6FuvyTEDVAKUius4gUF1ocWdRCMXGuDoXFudg3OzO55+ad5Wm2qdYAkBYZpEyf4wH0sRabGD1lSjXWv6xl4Yy6ZkyFxsbK4Kx05vq//CcvLlVZUfycl0hm9hTcjjCwohbnn4orTm4xzALaEgOGk2JH3C+CR9g30V6z07bjAYyb/f2OTh7Lfs+eJ0y83xJXlcV+3jRNU+sXkY0VWPDUknVcMhvadl9O9m+loTV0bPXr1j4ssG18TTZZO2/qCEUyO5pOLpLJ0BTRhSkXPBs3GvkCk8IC+HsYSvcOOMAsfg==

Steps to reproduce

Juggle between passing the invalidPropType and validPropType to the component and observe the warning message

What is expected?

The warning message should contain the expected class' name

What is actually happening?

Warning message contains an empty string

System Info

No response

Any additional comments?

The issue comes from the getType function (

const match = ctor && ctor.toString().match(/^\s*function (\w+)/)
) attempting to match the toString() result for a function keyword. This fails for classes since they return a class [NAME] {} when stringified. Couldn't we just use ctor.name here? It returns Object when passed an object, Array when passed an array etc, I don't know if it's widely supported though

@dotSILENT dotSILENT changed the title Empty type name when expected prop type is class constructor Empty type name in warning when expected prop type is class constructor Nov 23, 2022
@dotSILENT dotSILENT changed the title Empty type name in warning when expected prop type is class constructor Empty type name in warning when expected prop type is a custom class Nov 23, 2022
@LinusBorg
Copy link
Member

LinusBorg commented Nov 23, 2022

@LinusBorg LinusBorg reopened this Nov 23, 2022
@dotSILENT
Copy link
Author

dotSILENT commented Nov 23, 2022

This happens when TS is not used and this method of defining props with custom classes is even referenced in Vue's guide to prop validation: https://vuejs.org/guide/components/props.html#runtime-type-checks

@LinusBorg
Copy link
Member

Already realized my mistake, sorry about that.

@LinusBorg
Copy link
Member

This function returns an empty string for custom classes because MyClass.toString() does not match the regexp when using real native ES6 classes. Should be easy enough to extend this to also work for custom classes.

function getType(ctor: Prop<any>): string {
const match = ctor && ctor.toString().match(/^\s*function (\w+)/)
return match ? match[1] : ctor === null ? 'null' : ''
}

@LinusBorg LinusBorg added the 🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed. label Nov 23, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Sep 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants