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

Generator expects key type K of int|string, K of int|string given. #7484

Closed
RikudouSage opened this issue Jun 15, 2022 · 9 comments · Fixed by phpstan/phpstan-src#1433
Closed
Labels
Milestone

Comments

@RikudouSage
Copy link
Contributor

Bug report

Generator expects key type K of int|string, K of int|string given.

Code snippet that reproduces the problem

https://phpstan.org/r/11599e77-4519-411d-b01f-77a89226a09c

Expected output

No error.

@rvanvelzen
Copy link
Contributor

The error message is ambiguously formatted, but it is correct. This is what it's actually saying: Generator expects key type (K of int)|string, K of int|string given..

Parenthesization should be improved.

@RikudouSage
Copy link
Contributor Author

@rvanvelzen Doesn't it boil down to expecting int or a string? Which I'm providing?

@rvanvelzen
Copy link
Contributor

You're not yielding a key of K because you're changing the type if it's a string. You can resolve it by using a conditional type: https://phpstan.org/r/e466fec7-aa7b-45e0-9c94-1c3503da9a00

@RikudouSage
Copy link
Contributor Author

That doesn't make sense to me... I'm saying that it returns int or string and that it's the same as what the original array has. Strtoupper and strtolower don't change string into something else, they return string.

@rvanvelzen
Copy link
Contributor

strtolower and strtoupper change K to string, which is not the same as K.

One way of looking at it is that K refers to the value, so if you modify the value the type does not remain K.

@RikudouSage
Copy link
Contributor Author

I understand what you're saying, it just doesn't make sense to me. That function basically accepts iterable<int|string, mixed> and returns iterable<int|string, mixed>.

I used the template only for the added benefit of knowing that if you provide int keys you get int keys back. K is a type, not a value and the type returned is correct.

@ondrejmirtes
Copy link
Member

Yes, that's why you need a conditional return type and not generics.

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants