-
Notifications
You must be signed in to change notification settings - Fork 668
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 InvalidArgument when using generic arrays of multiple value types #6563
Comments
I found these snippets: https://psalm.dev/r/e80523194f<?php
/**
* @template T
*/
final class Node {
/**
* @param T $value
*/
public function __construct(public mixed $value) {}
}
/**
* @template Tk of array-key
* @template Tv
*
* @param array<Tk, Node<Tv>> $_array
*/
function a(array $_array): void {}
a(['a' => new Node('a')]);
a(['a' => new Node('a'), 'b' => new Node(3)]);
|
actually, not a bug, but the error message should be clearer ( Node is not covariant ). |
I found these snippets: https://psalm.dev/r/991ca76408<?php
/**
* @template-covariant T
*/
final class Node {
/**
* @param T $value
*/
public function __construct(public mixed $value) {}
}
/**
* @template Tk of array-key
* @template Tv
*
* @param array<Tk, Node<Tv>> $_array
*/
function a(array $_array): void {}
a(['a' => new Node('a')]);
a(['a' => new Node('a'), 'b' => new Node(3)]);
|
I'll close this, the message was improved slightly |
Seems like this could still be improved to work like this without requiring the |
This comment has been minimized.
This comment has been minimized.
@AndrolGenhald The real fix is: https://psalm.dev/r/45db240ac3 The message was cryptic before, now it explains the issue, but it doesn't show the possible solution |
This comment has been minimized.
This comment has been minimized.
I think it's also fine to widen the type even when the template isn't covariant, but I guess I'm fine requiring that to be an explicit annotation. I know we've talked about some sort of alternate annotation to |
I found these snippets: https://psalm.dev/r/2c5a043897<?php
/**
* @template T
*/
final class Node {
/**
* @param T $value
*/
public function __construct(public mixed $value) {}
}
/**
* @template Tk of array-key
* @template Tv
*
* @param array<Tk, Node<Tv>> $_array
*/
function a(array $_array): void {}
a(['a' => new Node('a')]);
/** @var list<Node<'a'|3>> */
$nodes = ['a' => new Node('a'), 'b' => new Node(3)];
a($nodes);
https://psalm.dev/r/45db240ac3<?php
/**
* @template-covariant T
*/
final class Node {
/**
* @param T $value
*/
public function __construct(public mixed $value) {}
}
/**
* @template Tk of array-key
* @template Tv
*
* @param array<Tk, Node<Tv>> $_array
*/
function a(array $_array): void {}
a(['a' => new Node('a')]);
$nodes = ['a' => new Node('a'), 'b' => new Node(3)];
a($nodes);
|
see: https://psalm.dev/r/e80523194f
expected: no errors, the array passed is of type
array<string, Node<string>|Node<int>>
The text was updated successfully, but these errors were encountered: