-
-
Notifications
You must be signed in to change notification settings - Fork 201
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
Optional returns the string | undefined type even with the default value set #286
Comments
I have tested your schema in my playground and get the following input and output type: type ConfigInput = {
logger: {
level?: string | undefined;
path?: string | null | undefined;
};
}
type ConfigOutput = {
logger: {
level: string;
path: string | null;
};
} Which version of Valibot are you using? Can you provide more code so I can investigate the problem? Do you have |
Now I also noticed that path is always equal to a string, although it can be both a string and null |
Is |
Just noticed that the question marks are missing in |
Thank you very much for your work and such a quick response. |
v0.24.0 is available. Can you check if the problem still exists on your end? |
You are right. I am sorry. I will fix that. |
v0.24.1 is available. Can you check if it works now? I will soon add unit tests for the types to prevent this from happening again. |
I'll leave my code so you don't have to rewrite it. |
Do you think it could be the version of node being used? |
I don't think so. I also use Node.js v18. It must be related to TypeScript (I use v5.2.2). |
I'm using 5.3.2 |
TypeScript v5.3.2 produces the same results in my editor. Can you clone the Valibot repo and copy and past the following into import * as v from './mod.ts';
export const ConfigSchema = v.object({
logger: v.object({
level: v.string(),
format: v.optional(v.string(), '{icon} > {time} | {level} | {message}'),
path: v.optional(v.nullable(v.string()), null),
}),
});
export type Config = v.Output<typeof ConfigSchema>; |
You can run |
I suspect the bug in the |
I did exactly that. After building with pnpm, I imported from dist and the error returned. I showed it in my second screenshot. |
This is strange. I don't know what to do. I reinstalled Valibot in another project and tested the code there. Everything works as it should. Since I can't reproduce it, I have no way to debug it. 😐 |
I created the project on a completely different machine and got the same result I use tsconfig by default
My clear project: |
I can partially reproduce this problem in a WebStorm IDE. (You can see the type of But I do think this is an IDE bug (or mis-configuration, I don't use WebStorm so I don't know). Moreover, if the If this is an IDE bug, this shouldn't affect the build process, and I can confirm no type check errors are emitted when I use So you can still build your project without issues. And everything works correctly under VSCode. Here is my test code: import * as v from "valibot";
export const ConfigSchema = v.object({
logger: v.object({
level: v.string(),
format: v.optional(v.string(), "{icon} > {time} | {level} | {message}"),
path: v.optional(v.nullable(v.string()), null),
}),
});
declare function expectString(_: string): void;
declare const config: v.Output<typeof ConfigSchema>;
expectString(config.logger.format); |
@Sec-ant You're damn right. I switched to VS Code myself a few hours ago and noticed that the problem was solved. |
@fabian-hiller The problem was indeed solved in version 2.24.1 I'm sorry for wasting your time. |
No problem. Thanks a lot that you found this bug! |
I use the following schema:
When I try to pass the value after validation, I get an error like:
TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'.
Type:
level?: string | undefined
This is a strange behavior, because when the default value is set, either the value from json or the default value will be returned to me.
The text was updated successfully, but these errors were encountered: