Skip to content
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

fix(library): correct type inference in ArgsAction #976

Closed

Conversation

shulaoda
Copy link

@shulaoda shulaoda commented Dec 12, 2024

Actually, I found that type Output = v.InferOutput<typeof ComplexSchema> will be deduced as:

type Output = {
    fn: (args_0: string) => boolean;
    argsWithFn: (args_0: (...args: unknown[]) => unknown) => unknown;
}

Is this the expected behavior? Is there a problem with my writing for the args_0 of the argsWithFn?

Originally posted by @shulaoda in #968 (comment)

The second generic of BaseTransformation should be InferOutput<...>.

Playground: https://valibot.dev/playground/?code=JYWwDg9gTgLgBAKjgQwM5wG5wGZQiOAcg2QBtgAjCGQgbgCh6BjCAO1XgGF8xSBTAB4BlJgAs+IZHAC8mAHQcowJjADyFAFZ8VACgDe9ODlYAueWGBg+Ow0fnYArqxXA2OgJQAaW0YxzkUADmqDp+MA681gDaforArIEeALruXj7yUHzhUOyhclQQ-MisHmlGZShBqADqwDCiAGKm5pbW6X6OzjCuJRW+-lU2dnZhEfw6UenDfhZWQ8MLHU4ubn0L-QHBeeGRE34FRb0pa+t+mdm5sTBKCaXe6+X3CylPj+1y5w45IVc3ialPNIAX3cDHoMAAnlY4KoHDAwHCZPIAJKsbB8KCw+FwgA8kKsEGwcG44H4wjEEmQAD4wSx2PAIHCETAzFjmUiDEZsM0dJszHEEu4ZFS4Ho4J8cnB8XxCZVAjJpLJCALAoQ4ECnpsanVGjzuWZeUEzAc+MUhdIRSrzSKxRLWMYdNgyKg+EKgfR3cw2KhCnw5KQIIlGdiYO56EA

@fabian-hiller
Copy link
Owner

I think there was a reason why I choose InferInput. I will investigate it to check if it is a bug and report back to you. Thank you!

@fabian-hiller fabian-hiller self-assigned this Dec 12, 2024
@fabian-hiller fabian-hiller added question Further information is requested priority This has priority labels Dec 12, 2024
@fabian-hiller
Copy link
Owner

Hey, I think the current implementation with InferInput is correct and your change with InferOutput would be a bug. The reason for this is that when you call the function, the argument you pass must match the input of the schema, not the output. The argument schema will then parse the input and pass its value to the function. I know this is hard to follow, but here is an example that demonstrates this case.

import * as v from 'valibot';

const StringToNumberSchema = v.pipe(
  v.string(),
  v.decimal(),
  v.transform(Number)
);

const FunctionSchema = v.pipe(
  v.function(),
  v.args(v.tuple([StringToNumberSchema])),
  v.returns(v.number())
);

const doubleNumber = v.parse(FunctionSchema, (value: number) => value * 2);

const result = doubleNumber('2.5'); // <-- The arg must be a `string` not a `number`
console.log(result); // 5

@shulaoda
Copy link
Author

Got it. Thanks again!

@shulaoda shulaoda deleted the fix-error-ts-type-in-args-action branch December 23, 2024 06:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority This has priority question Further information is requested
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants