-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
OneOf
type for interface without discriminators
#23928
Comments
Not sure this type is what you want then. Looks like the type you are looking for is Since the union does not have a discriminant (i.e. a common property with a unit type that is different in each constituent), the only way to narrow it is using declare var foo: { size: number } | { horizontalSize: number; verticalSize: number; };
"size" in foo ? foo.size : foo.horizontalSize; |
You might have better luck sharing this on StackOverflow. This is not a support forum, it is one for reporting compiler/language bugs and suggestions. |
@mhegazy Thanks for the tip! I didn't want to word the ticket as a bug because I wasn't sure whether that was expected TS behaviour or not; it definitely surprised me. Using |
Actually, apologies for another ping, @mhegazy, but there is a Typescript bug in here I think which surfaces when I use the
In the above I would think |
Looks like #20863 already tracks that (took me a while to find it). |
I have the following type
The idea is that I could use it when I have an interface that can have one combination of props or the other (e.g.
size: number
vshorizontalSize: number; verticalSize: number
).Now, I would think
Would have type
number
but it actually hasnumber | undefined
.Should TypeScript be able to determine that the other "branch" should be used (i.e.,
size
is missing ergohorizontalSize
must be defined)?The text was updated successfully, but these errors were encountered: