-
Notifications
You must be signed in to change notification settings - Fork 224
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
optional() does not create an optional property on an object #375
Comments
Ah interesting, that's unfortunate. I didn't actually realize they were treated differently, but I agree that it should support the The issue is I'm not sure how to "lift" that typing logic up from the property's struct definition into the object's, since it's only something that applies at the object level and doesn't exist at the single-value level. |
Hi Ian, yes I can make a PR though not sure the timeline. Unfortunately, the lift will be a little hacky in that anything that has a union with FYI, it is impossible to get optional types strictly correct due to this limitation of TypeScript microsoft/TypeScript#13195 |
@thesunny was just running into this myself. I'm not sure there is a way to pull up the optionality with TypeScript, but if there is I'd be open to it. But I did realize a workaround we can use: export const Text = intersection([
object({
text: string(),
}),
partial({
bold: literal(true),
}),
]) |
Oh nice! Thanks for sharing that. To clarify my previous post, there is a way to do it by turning all unions with |
Ah interesting, that seems like a decent trade off I think. Seems pretty rare that you’d explicitly want a defined |
I just ran in to this issue, thank you for the workaround. Is it likely your proposed solution will make it in @thesunny? The workaround is fine, but it would be nice to be able to avoid the extra clutter in the struct definition and resulting typescript def. Loving your work on Superstruct, it's a joy to use. |
This now has a PR: #390 |
optional
does not create an optional property on an object. It should create an optional property in order to match with TypeScript validation semantics.Alignment with TypeScript
The Superstruct type does not align with the TypeScript type it is returning.
As an example, this passes Superstruct validation:
but fails the type
number | undefined
returned by Superstruct:The red squiggles indicate the following error when hovered:
Use case
My use case is that I want to validate slate data types.
They are defined such that the
bold
property is optional. To be specific, they areoptional(literal(true)
. In order to make sure my validations line up against my type definitions exactly, I use unit testing to make sure the two type are equal. Because the optional types show up as a union of the passed in type and undefined but do not add the?
to the property, they do not match and fail tests.The text was updated successfully, but these errors were encountered: