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

Using union with generic types in a parameter results in wrong return type #11193

Open
shira-374 opened this issue Dec 28, 2024 · 1 comment
Open

Comments

@shira-374
Copy link

I'm trying to implement a kind of "auto-boxing" of generic result objects. If a non-result value gets passed, it is automatically wrapped:

/**
 * @template TNextValue
 * @template TNextError
 *
 * @param Result<TNextValue, TNextError>|TNextValue $result
 * @return Result<TValue|TNextValue, TNextError>
 */
abstract function orElse($result): Result;

However, I'm getting a wrong return type when I actually call this method:

https://psalm.dev/r/40be01250b

It seems to work fine in PHPStan: https://phpstan.org/r/c9779525-79a6-4a8f-b5f5-9d1c652c204f

I don't think the |TNextValue part should be considered in this case, because Result<TNextValue, TNextError> is more specific.


For completness, my actual code involves closures (and exhibits the same issue):

https://psalm.dev/r/9880a39426

Also works in PHPStan: https://phpstan.org/r/cd2248d1-2862-477d-ba0a-65ab69445f25

Copy link

I found these snippets:

https://psalm.dev/r/40be01250b
<?php declare(strict_types=1);

/**
 * @template-covariant TValue
 * @template-covariant TError
 * @psalm-suppress InvalidTemplateParam not relevant
 */
abstract class Result
{
    /**
     * @template TNextValue
     * @template TNextError
     *
     * @param Result<TNextValue, TNextError>|TNextValue $result
     * @return Result<TValue|TNextValue, TNextError>
     */
    abstract function orElse($result): Result;
}

/**
 * @param Result<int, string> $result
 * @param Result<never, float> $elseResult
 * @psalm-suppress UnusedVariable not relevant
 */
function example(Result $result, Result $elseResult): void
{
    /**
     * @psalm-trace $x
     * @psalm-check-type $x = Result<int, float>
     */
    $x = $result->orElse($elseResult);
}
Psalm output (using commit 765dcbf):

INFO: Trace - 31:5 - $x: Result<Result<never, float>|int, float>

ERROR: CheckType - 31:5 - Checked variable $x = Result<int, float> does not match $x = Result<Result<never, float>|int, float>
https://psalm.dev/r/9880a39426
<?php declare(strict_types=1);

/**
 * @template-covariant TValue
 * @template-covariant TError
 * @psalm-suppress InvalidTemplateParam not relevant
 */
abstract class Result
{
    /**
     * @template TNextValue
     * @template TNextError
     *
     * @param Closure(TError, mixed...):(Result<TNextValue, TNextError>|TNextValue) $callback
     * @return Result<TValue|TNextValue, TNextError>
     */
    abstract function orElse(\Closure $callback): Result;
}

/**
 * @param Result<int, string> $result
 * @param Closure():Result<never, float> $callback
 * @psalm-suppress UnusedVariable not relevant
 */
function example(Result $result, Closure $callback): void
{
    /**
     * @psalm-trace $x
     * @psalm-check-type $x = Result<int, float>
     */
    $x = $result->orElse($callback);
}
Psalm output (using commit 765dcbf):

INFO: Trace - 31:5 - $x: Result<Result<never, float>|int, float>

ERROR: CheckType - 31:5 - Checked variable $x = Result<int, float> does not match $x = Result<Result<never, float>|int, float>

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