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

bug: transform output type inside an object is broken on v0.39 #806

Closed
cruzdanilo opened this issue Aug 29, 2024 · 3 comments
Closed

bug: transform output type inside an object is broken on v0.39 #806

cruzdanilo opened this issue Aug 29, 2024 · 3 comments
Assignees
Labels
fix A smaller enhancement or bug fix priority This has priority workaround Workaround fixes problem

Comments

@cruzdanilo
Copy link

in v0.39, the output type of an object field with a transform is not working properly, returning the original type before the transformation. execution works as expected, but the typescript compiler fails. here is a reproduction example:

import { array, nullish, object, parse, pipe, string, transform } from "valibot";

function f({ a }: { a: string[] }) {
  console.log(a);
}

const Schema = object({ a: pipe(nullish(array(string())), transform((x) => x ?? ["default"])) });


const output = parse(Schema, { a: null });

f(output);

execution output:

[ 'default' ]

tsc output:

index.ts:12:3 - error TS2345: Argument of type '{ a?: string[]; }' is not assignable to parameter of type '{ a: string[]; }'.
  Property 'a' is optional in type '{ a?: string[]; }' but required in type '{ a: string[]; }'.

12 f(output);
     ~~~~~~

i pushed this same repro to a git repo: https://github.com/cruzdanilo/repro-valibot-transform
you can see this same output in the ci workflow log: https://github.com/cruzdanilo/repro-valibot-transform/actions/runs/10614474287/job/29420466950

@fabian-hiller
Copy link
Owner

Thank you for creating this issue. I recommend that you change your schema. This will also fix this problem. However, I will investigate this problem and may change the current implementation.

You can try my code in this playground.

import * as v from 'valibot';

const Schema = v.object({
  key: v.nullish(v.array(v.string()), ['default']),
});

type Input = v.InferInput<typeof Schema>;
type Output = v.InferOutput<typeof Schema>;

@fabian-hiller fabian-hiller self-assigned this Aug 29, 2024
@fabian-hiller fabian-hiller added workaround Workaround fixes problem priority This has priority fix A smaller enhancement or bug fix labels Aug 29, 2024
@fabian-hiller
Copy link
Owner

I agree. When an optional input is transformed to a non-optional value, the output type should reflect this information. I have improved the implementation and added new type test. Thanks for your feedback!

@fabian-hiller
Copy link
Owner

v0.40.0 is available 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix A smaller enhancement or bug fix priority This has priority workaround Workaround fixes problem
Projects
None yet
Development

No branches or pull requests

2 participants