Closed ilxqx closed 4 months ago
Here is a Demo code zip package that can quickly reproduce the problem. After downloading, open with VSCode and run pnpm i && pnpm build
reproduce it.
Hello it's probably a path resolution issue. I open a PR to try to solve it (#475).
Could you try to install the following snapshot version and let me know if it works?
pnpm add vue-types@0.0.0-20240701051251
Hello it's probably a path resolution issue. I open a PR to try to solve it (#475).
Could you try to install the following snapshot version and let me know if it works?
pnpm add vue-types@0.0.0-20240701051251
@dwightjack Good, after installing this snapshot version, everything is fine after building. πππ Thanks very much.
I have another question that I need your help with: How can I specify custom error messages for custom validation types created by toType/toValidableType/formType
? Do I have to use the custom
function to specify custom error messages?
I checked the documentation for vue-types but couldn't find any relevant information.
I hava a customized validation type func:
/**
* Returns a prop type definition for a color value.
*
* @returns The prop type definition.
*/
export function color() {
return toType(
"color",
{
type: String,
validator: value => colord(value).isValid(),
},
);
}
When an error is triggered, the browser console will issue the following warning:
However, when there are more and more custom validation types in my project, I can't clearly remember what specific errors should be indicated to me as shown in the screenshot above. So I need to customize the error message to indicate the specific reason for violating the prop constraint. In the screenshot above, it's a color format issue rather than something else.
It seems that I have a way of doing this, the way below. Is this approach the best practice?
/**
* Returns a prop type definition for a color value.
*
* @returns The prop type definition.
*/
export function color<T extends string = string>() {
return toValidableType<T>(
"color",
{
type: String as unknown as PropType<T>,
validator(value) {
const isValid = colord(value).isValid();
if (!isValid) {
console.warn(`color - "${value}" is not a color`);
}
return isValid;
},
},
);
}
@ilxqx It is not so easy to customize the warning of the custom validator because it's managed by Vue internals. I think your solution is the simplest and most effective one.
A probably more elegant but complex approach would be to set a custom warning handler. The downsides are:
warnHandler
will intercept every warning generated from the application, so you need to filter the warnings you want to customize. πππNice, thanks very much
@dwightjack Hi, can a new release be made for this issue? Thanks.
@ilxqx sorry for the delay. Released as vue-types@5.1.3
. π
Library version 5.1.2
Vue.js version 3.4.31
Question Hi, Thanks very much for your work. Here is my Button component props definition code snapshot:
My build types command:
vue-tsc -p ./tsconfig.build.json
. tsconfig.build.json:My tsconfig.app.json:
The Button component props type definition file after building:
Here is the snapshot:
Restarting VSCode does not solve this problem. This makes me very confused, can you help me? Thanks in advance πππ.