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

Cannot combine generic key types when @template is limited to array-key #10871

Open
Shira-3749 opened this issue Apr 13, 2024 · 0 comments
Open

Comments

@Shira-3749
Copy link

Bug report

When I define a generic key type as @template TKey of array-key, PHPStan doesn't seem to want to combine it with other types via @return:

/**
 * @template TOtherKey of array-key
 * @template TOtherValue
 *
 * @param iterable<TOtherKey, TOtherValue> ...$iterables
 * @return self<TKey|TOtherKey, TValue|TOtherValue>
 */
function merge(iterable ...$iterables): self;

Calling this method does not combine TKey with TOtherKey - it just keeps it as TKey:
https://phpstan.org/r/31d3d483-7e0c-4239-8c88-67cf00c807ae

This seems to work fine in Psalm: https://psalm.dev/r/8671dd080b

Using string|int instead of array-key seems to "fix" this:
https://phpstan.org/r/edf51290-fe27-4024-b07d-3e7c327e5825

And so does removing the type entirely:
https://phpstan.org/r/ff4f41fa-691c-4fe4-8837-ef0ec2179c04

Code snippet that reproduces the problem

https://phpstan.org/r/31d3d483-7e0c-4239-8c88-67cf00c807ae

Expected output

-Dumped type: Map<string, bool|int>
+Dumped type: Map<int|string, bool|int>
-Dumped type: Map<string, bool|int|stdClass>
+Dumped type: Map<int|string, bool|int|stdClass>

Did PHPStan help you today? Did it make you happy in any way?

No response

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