-
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
How do I make this work as expected? #11179
Comments
I found these snippets: https://psalm.dev/r/a2c22ba1dd<?php
/**
* @template TCallback of int|float
*
* @param callable():TCallback $callback
*
* @return TCallback
*/
function sum(callable $callback, array $items): int|float
{
return array_sum(array_map($callback, $items));
}
|
You need to cast the return value of |
I found these snippets: https://psalm.dev/r/fed7db1b92<?php
/**
* @param callable(mixed):T $callback
* @return T
* @template T of int|float
*/
function sum(callable $callback, array $items): int|float
{
/** @var T $result */
$result = array_sum(array_map($callback, $items));
return $result;
}
$_sum_i = sum(static fn (int $i): int => $i, [1, 2, 3]);
$_sum_f = sum(static fn (float $i): float => $i, [1, 2, 3]);
/** @psalm-trace $_sum_f $_sum_i */
|
Cool, thanks! Is this expected? I would expect that if a type is either |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://psalm.dev/r/a2c22ba1dd
I would like
TCallback
to either be anint
orfloat
. If I use thenumeric
type it can also benumeric-string
which is not intended.Can I make this work somehow?
The text was updated successfully, but these errors were encountered: