-
-
Notifications
You must be signed in to change notification settings - Fork 216
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
Feat start with types #986
base: main
Are you sure you want to change the base?
Conversation
Thanks for creating this PR. The idea is great. Right now Issue #945 is related. |
Fixed an issue to stop piping the TInput. I think I align with validation transforming the output types, even if the runtime value is unaffected. I believe the validation should serve as a way of effectively narrowing the type. The input for the parse methods already transform the input type, since they can take |
Thank you for your feedback. I will further investigate it in the next few weeks. |
I though about this change and noticed a big problem. Have a look at the following example: const Schema = v.pipe(v.string(), v.startsWith('foo@'), v.email()); If the |
I am not sure I understand what you mean. My PR does not make any runtime changes to the validation. Why would narrowing the output type force us to mark the output as untyped? Furthermore, even if subsequent actions require the type, the input type on the validation actions in pipe should always be the type defined by the schema, and not the previous validation. Therefore, it should not rely on the output type of previous validation. I do understand why that behaviour may not be intuitive. Is there any way we can have some ability to opt into this more accurate narrowing? |
The problem is this. The data type of a pipeline item is based on the previous item. So in the following example, the input type of const Schema = v.pipe(v.string(), v.startsWith('foo@'), v.email()); But if we change the output type of In simple words: If |
At the moment I see one possible workaround and one solution. The workaround would be to manually transform to the expected type. This works because a transformation is only executed if there was no issue before. So whenever const Schema = v.pipe(
v.string(),
v.startsWith('foo@'),
v.transform((input) => input as `foo@${string}`)
); A possible solution would be to provide a Due to the type safety of this library, there is a necessary and important difference in the behavior of validation actions and transformation actions. |
Ah, I do start to understand the issue now. Although it is not fully clear why we are forced to mark the output as untyped instead of the last successful validation step (in example caching it until the current validation is successful) |
I agree. This is probably easy to solve using pure JavaScript, but I am not sure how to make it type safe using TypeScript. 😐 I am happy to get more feedback, also from others, but in the meantime, this is something we will probably reconsider after the release of v1. |
Changes the output type for
startsWith
andendsWith
to actually reflect.Previously:
startsWith("abc")
-- output -->string
Now:
startsWith("abc")
-- output -->abc${string}
Similarly with endsWith.