Skip to content
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

Incompatible types when a list is expected in a compound type and Psalm type inference produces a list shape instead #11196

Open
emmanuelGuiton opened this issue Jan 8, 2025 · 1 comment

Comments

@emmanuelGuiton
Copy link
Contributor

In the following snippet, a function takes a parameter typed T<list<V>>. Psalm throws an error if we pass a valid variable that has just been initialized from a constant list of values. Psalm interprets the list type as a list shape T<list{V}> and considers it incompatible with the expected type T<list<V>>.

https://psalm.dev/r/86f88fbdda

Copy link

I found these snippets:

https://psalm.dev/r/86f88fbdda
<?php

/** @template T */
class AClass {
    /** @psalm-param T $value */
    public function __construct(public $value) { }
}

class AnotherClass {}

/**
 * @psalm-param AClass<list<AnotherClass>> $aClassInstance
 * @psalm-suppress UnusedParam
 */
function aFunction(AClass $aClassInstance): void {
}

aFunction(new AClass([new AnotherClass()]));

/** @psalm-return list<AnotherClass> */
function aList(): array {
     return [new AnotherClass()];
}
aFunction(new AClass(aList()));
Psalm output (using commit 765dcbf):

ERROR: InvalidArgument - 18:11 - Argument 1 of aFunction expects AClass<list<AnotherClass>>, but AClass<list{AnotherClass}> provided

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant