-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Incorrect logical-or-operator type inferred with optional values and an empty object #49710
Comments
This is due to subtype reduction wherein |
Additionally, when explicitly typing it also accepts |
Likely some confusion on this point - a string is a valid |
Ah I don't think I've run into this concept before. That makes sense, so basically this is pretty much the same as something like this: interface A {
a: string;
}
interface B extends A {
b: string;
}
const getA = (): A | undefined => ({ a: 'a' });
const getB = (): B => ({ a: 'a', b: 'b' });
const explicit: A | B = getA() || getB();
// implicitly typed as `A`
const implicit = getA() || getB(); It's just that the empty object is a supertype for so many things. I think I read somewhere about the concept of "explicit" empty object type. Something like that would probably help here. I suppose I could easily use something like |
Yep, itβs the same mechanism in play. |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
π Search Terms
empty object, infer, or operator, invalid type
π Version & Regression Information
β― Playground Link
Playground link with a very simple example
π» Code
π Actual behavior
When I use the logical or-operator to assign a value from a list of values that can be
undefined
and use the empty object{}
as the last parameter the type gets inferred to only be the empty object instead of all the optional values that preceded it.π Expected behavior
I would expect the other types to be preserved. If I explicitly type the variable so that it can contain the preceding types as well as an empty object the type doesn't get flattened to just
{}
. If that were the case then I would expect the flattening to happen with the or-operator as well.The use case where I ran into this was when I am receiving a list of objects from multiple sources and wanted to pick the first non-null value. However once I then do
if ('xxx' in yyy)
type of narrowing the object can potentially be null which prompted me to add the last default value as{}
so that I could do thein
check without worrying about null values.Obviously I can work around this by using explicit typing or adding some bogus property to the empty object to make the inferring work but I felt that this is not how it's supposed to work. Since this is such a simple scenario I'm guessing this has been reported before and is expected functionality but I was not able to find any issue regarding this so I decided to open this.
The text was updated successfully, but these errors were encountered: