-
Notifications
You must be signed in to change notification settings - Fork 663
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
object types with optional properties don't get their inferred type changed after isset() check #5077
Comments
I found these snippets: https://psalm.dev/r/9560e1b060<?php
/**
* @param object{
* bar?: object{
* baz?: string
* }
* }|null $foo
* @return void
*/
function testSparce($foo)
{
// This should change the inferred type, no?
if (isset($foo->bar->baz))
{
testConcrete($foo);
}
}
/**
* @param object{
* bar: object{
* baz: string
* }
* } $foo
* @return void
*/
function testConcrete($foo)
{
echo "" . $foo->bar->baz;
}
|
I think this is the same issue I just reproduced here: |
I found these snippets: https://psalm.dev/r/c90854d4fc<?php
/**
* @param array{a?: array{foo: bool, bar: bool}} $arr
*/
function foo(array $arr):void {
$a = $arr['a'] ?? [];
if ($a){
bar($a);
}
}
/**
* @param array{foo: bool, bar: bool} $arr
*/
function bar(array $arr):void{
}
|
I think it's because the type-checker converts our optional array into an array where each key is optional - thus even when the original key is determined to exist, the types of it's content have changed to an array of optional keys. I presume the fix therefore is to resolve that an array with non-optional keys that is a type of an optional key in a parent array doesn't have that behaviour implied. |
This issue is very related to #2206 @M1ke your case is different though. Psalm is not fine enough to understand that you basically have |
After that isset() check, I would expect psalm to change its interpretation of the type of
$foo
to know that all the optional parameters are set. The call totestConcrete($foo)
should work here. Instead, it flags this error:https://psalm.dev/r/9560e1b060
The text was updated successfully, but these errors were encountered: