-
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
Function return typing not working well when depth is higher than 1 #52893
Comments
Duplicate of #46449 |
The problem is related to the linked issue (as the problem is related to the union reduction) - but it still feels to me that those 3 code samples reduce differently. |
Optional inference only happens when all input candidates are fresh object literals. See #19513 |
I was expecting that freshness is at play here but the thing that still surprises me is that |
I guess you could argue that literals with spreads shouldn't be considered fresh, since the sorts of guarantees that literals provide don't usually apply when a spread is present. |
I think that even with spreads the object literal should be treated as fresh if all of its "spread sources" are object literals. This might matter for conditional object creation like in those patterns: const obj1 = {
a: 1,
...(bool ? { b: 2 } : { c: 3 })
}
const obj2 = {
a: 1,
...(bool && { b: 2 })
} That being said, I'm still wondering what the rules are here :P If I understand you correctly then you are saying that the spread case is actually fresh today - but then, what is the rule that makes the |
Sorry, I got something backwards above - the one with spreads is working correctly. The difference between 2 and 3 is just whether subtype reduction has happened yet. I'm not really sure why one happens and not the other; subtype reduction is allowed to happen anywhere, but is not required to do so at any time, so either behavior is considered correct from our perspective. |
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
Function Return, Optionality, Optional
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
Result 1 doesn't have optional attribute c
π Expected behavior
All results should have optional attribute c
The text was updated successfully, but these errors were encountered: