-
Notifications
You must be signed in to change notification settings - Fork 17
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
Hard to debug union types #5
Comments
Hey @wasd171. Thanks for the issue. If I understand correctly, instead of:
You would like a more explicit error, like in your second example:
I will have a look at the io-ts context we're getting back, but it may be that io-ts is not giving us enough detail on the context object here. |
Thank you. |
We do seem to have the detail we need to achieve this level of reporting. To achieve more specific error reporting, we would need to identify which type in the union has the closest match (in this case the I wonder how we could achieve this identification… 🤔 In your example, it would be quite simple because the value we're validating is an For reference, here is the value we get back from io-ts. [
{
"value": {
"foo": 17
},
"context": [
{
"key": "",
"type": {
"_A": null,
"_tag": "UnionType",
"name": "({ foo: string } | null)",
"types": [
{
"_A": null,
"_tag": "InterfaceType",
"name": "{ foo: string }",
"props": {
"foo": {
"_A": null,
"_tag": "StringType",
"name": "string"
}
}
},
{
"_A": null,
"_tag": "NullType",
"name": "null"
}
]
}
}
]
}
] |
I guess it's possible to just print the error for every type and let the user search. |
I have the same problem with unions. I was hoping to use io-ts for helping debug the places where our TypeScript code interfaces with JavaScript projects, but our union types are producing errors that are just as hard to understand as the errors that happened without validation: Expecting string at 0.meeting.attendees.0.displayName but instead got: undefined.
Expecting true at 1.hasOrder but instead got: false.
Expecting string at 1.meeting.attendees.0.displayName but instead got: undefined.
Expecting { hasResources: true, hasCatering: false, remoteOrderUid: string, meeting: { subject: string, meetingTime: { start: string, end: string }, attendees: Array<{ displayName: string, emailAddress: string, recipientType: (keyof ["user","distributionList","externalUser","other","unknown"]), appointmentResponse: (keyof ["none","organizer","tentative","accepted","declined","unknown"]), required: boolean }> }, resourceBooking: { resources: Array<{ id: number, name: string, resourceTypeId: number, serviceCenterId: number }> } } at 1.order.0 but instead got: undefined.
Expecting { hasResources: false, hasCatering: true, remoteOrderUid: string, meeting: { subject: string, meetingTime: { start: string, end: string }, attendees: Array<{ displayName: string, emailAddress: string, recipientType: (keyof ["user","distributionList","externalUser","other","unknown"]), appointmentResponse: (keyof ["none","organizer","tentative","accepted","declined","unknown"]), required: boolean }> }, cateringBooking: { deliveryTime: string, location: ({ type: "DELIVERY_TO_RESOURCE", resourceId: number } | { type: "FREE_TEXT_DELIVERY", location: string }), sittings: number, totalAmount: number, permaLink: string } } at 1.order.1 but instead got: undefined.
Expecting { hasResources: true, hasCatering: true, remoteOrderUid: string, meeting: { subject: string, meetingTime: { start: string, end: string }, attendees: Array<{ displayName: string, emailAddress: string, recipientType: (keyof ["user","distributionList","externalUser","other","unknown"]), appointmentResponse: (keyof ["none","organizer","tentative","accepted","declined","unknown"]), required: boolean }> }, resourceBooking: { resources: Array<{ id: number, name: string, resourceTypeId: number, serviceCenterId: number }> }, cateringBooking: { deliveryTime: string, location: ({ type: "DELIVERY_TO_RESOURCE", resourceId: number } | { type: "FREE_TEXT_DELIVERY", location: string }), sittings: number, totalAmount: number, permaLink: string } } at 1.order.2 but instead got: undefined. The first line is the actual error - I am missing displayName of the attendee. But all the other errors are wrong, they are the result of it being a union type. |
That would be really great 🎉 Personally as long it returns one error instead of several it would be enough. Eg. if a union is
I know that already for a simple example the error is long but it could be multiline instead:
This is already a good compromise IMO. But I don't know if it's hard/er to implement. I'm willing to give it a shot if you folks agree on it as a good (enough) solution. |
@gillchristian That would definitely be an improvement! |
@OliverJAsh is this still a thing you'd wanna merge? |
i'd like this too, so would love to help if I can! |
@gillchristian now that that PR is in could this potentially be closed in favor of more specific enhancements? Also, do you have privs to publish an update to NPM? I just made my own user-scoped package to consume it for now. |
cc @OliverJAsh |
No. I'll see if I can contact Oliver as he's not actively maintaining the project now (#17 (comment)).
Yes, I think that should be better 👌 Closed by #17 |
UPDATE: I do have access now to publishing on npm. But I have a problem to access my account, once that's sorted out I'll publish. |
Hello,
I have created a webpackbin to illustrate the problem. If union is not used, the reporter shows the problem exactly, if not, it just prints the whole expected type and provided value.
Check the console on webpackbin to see what I mean
The text was updated successfully, but these errors were encountered: