-
Notifications
You must be signed in to change notification settings - Fork 666
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
PropertyTypeCoercion when constructing WeakMap #7131
Comments
I found these snippets: https://psalm.dev/r/b3a63077ef<?php
declare(strict_types=1);
use WeakMap;
class Foo
{
/** @var WeakMap<SplFileObject, int> */
protected WeakMap $bar;
public function __construct()
{
$this->bar = new WeakMap();
}
}
|
At this point this is well-known limitation of Psalm generics. To work around this you have to provide the type information missing at the generic construction point using |
I found these snippets: https://psalm.dev/r/a0a3590bcd<?php
declare(strict_types=1);
use WeakMap;
class Foo
{
/** @var WeakMap<SplFileObject, int> */
protected WeakMap $bar;
public function __construct()
{
/** @var WeakMap<SplFileObject, int> */
$this->bar = new WeakMap();
}
}
|
Hi You say "well-known" but I wasn't able to find anything in the docs when I looked or in the existing issues list. Is there a link where I can find out more? |
I've started working on #10577 to fix this. |
When initialising an (appropriately annotated) property that is a WeakMap, Psalm complains about type coercion when calling the constructor. There cannot be any possibility of type coercion at that point, the newly constructed map is empty.
Reproducer: https://psalm.dev/r/b3a63077ef
Potentially a variation of #4055 ?
The text was updated successfully, but these errors were encountered: