-
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
Partially discriminated union with explicitly defined key loosens inferred type #55239
Comments
Pretty sure this is a duplicate of a few issues. This is not a bug; the underlying problem here is that You can actually see this in action by commenting out |
I guess I had assumed that the compiler would automatically pick the narrower type but being explicit with a negative type is probably the better approach. I'll close this as a dupe since it is the intended functionality. |
Woah, that's the first time I've seen the |
But that wouldn't be safe. Your type |
Bug Report
Please feel free to change the title to make it more accurate. I wasn't sure what to call this
π Search Terms
union narrow
π Version & Regression Information
5.1.6
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
For a partially discriminated union, when trying to access a narrowed field value, the compiler does not properly narrow the type. This forces the user to use the
in
operator to determine if the field exists. When instantiating the object though, the compiler does correctly infer that the field from the narrowed type needs to exist.π Expected behavior
I would expect the compiler to properly narrow the type if the explicitly defined union is set. In this case, if
tagName
isimg
, the compiler should infer thatimageSource
is required and available. IftagName
is any other string value, the compiler should fall back toElementStyle
for the type.The text was updated successfully, but these errors were encountered: