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

improve optionals guide #948

Merged

Conversation

EltonLobo07
Copy link
Contributor

Improvements:

  1. Replace Input and Output with InferInput and InferOutput in one of the examples.
  2. Change the behavior of the Dependent default values example to enable TypeScript to infer the correct output type.

More information related to the 2nd improvement:
The example without the improvement does not work as intended. This might be because TS does not narrow the type of a generic function parameter (related) but using the ternary operator narrows the type and everything works as intended. There is one major difference between the current and improved examples. The current example returned the same object reference but the improved example doesn't when sum is not undefined and I think it is fine because:

  1. The transform action is meant to transform values, so relying on referential equality should be discouraged.
  2. I wasn't able to find a better way to let TS infer the type of the output (without a type assertion or something similar).

Improvements:
    - Replace `Input` and `Output` with `InferInput` and `InferOutput`
      in one of the examples
    - Change the behavior of the `Dependent default values` example to
      enable TypeScript to infer the correct output type
Copy link

pkg-pr-new bot commented Nov 30, 2024

Open in Stackblitz

npm i https://pkg.pr.new/valibot@948

commit: bbdbc90

@fabian-hiller
Copy link
Owner

Thank you! I chose the current implementation to not clone the object unnecessarily for performance reasons when not needed. But your code is ok too. But let us simplify the code and remove the return statement by writing () => ({ ... }).

@fabian-hiller fabian-hiller self-assigned this Dec 4, 2024
@fabian-hiller fabian-hiller added the documentation Improvements or additions to documentation label Dec 4, 2024
@fabian-hiller fabian-hiller merged commit 42d6125 into fabian-hiller:main Dec 5, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants